title: SQL query email attachment - lots of spurious data in the output
body: <p>I'm running the following query as a scheduled job and sending the output as a query :</p>
  
  <pre><code>EXEC msdb.dbo.sp_send_dbmail
  @profile_name = 'SQL-Server',
  @recipients = 'XXXXX',
  @subject = 'Missing Essence File Check',
  @query = N'SELECT *   FROM ([omnibus].[dbo].[OmniBus_CLP_Clips] CLP 
  inner join [omnibus].[dbo].[OmniBus_CLD_ClipDetails] CLD on CLP.FLE_ID = CLD.FLE_ID) 
  inner join [omnibus].[dbo].[OmniBus_FLE_Files] fle on clp.FLE_ID = fle.fle_id
  where CLD_user_data  like '%.mov%'
  and CLD_user_data  not like '%.m2v%'
  and CLD_user_data  not like '%.wav%'
  and fle.FLE_file_deleted = 0;',
  @attach_query_result_as_file = 1,
  @query_attachment_filename = 'Missing Essence File Check.txt'
  </code></pre>
  
  <p>When I execute this query in SQL Server Management Studio, the output is a list of 8 digit ID numbers, or the message 'no rows affected'.  When I execute the query as a scheduled job and then email the results using Database Mail, I get all this additional data that I do not want or need.  Why does all this additional info appear in the output?  How do I get rid of it?  </p>
  
  <pre><code>*FLE_ID      CLP_clip_name                                                    CLP_clip_title                                                                                                                   CLT_ID      CLP_in_point CLP_out_point CLP_duratio
  n CLP_duration_frames CLP_tc_format_id CLP_complexity CLP_Locations                                                                                                                                                                                             
  
  
  
  rowversion CLP_WorkflowStatus   CLP_XTL                                                                                                                                                                                          
                                                                  CLP_InheritanceEnabled FLE_ID      CLD_vision_duration CLD_audio_duration CLD_prog_start CLD_video_mode CLD_audio_mode CLD_video_trans CLD_audio_trans CLD_wipe_mode CLD_wipe_num CLD_quality CL
  D_cache_mode CLD_logo    CLD_gpi     CLD_not_before CLD_not_after CLD_start_date          CLD_end_date            CLD_pdc     CLD_precompile CLD_archive_after CLD_delete_after CLD_suppress_logo CLD_size    CLD_version CLD_mirror  CLD_loop CLD_group_id CLD_
  status  CLD_use_caption CLD_use_subtitle CLD_record  CLD_clip_unavailable CLD_cg_command CLD_cg_next_page CLD_cg_version CLD_speed   CLD_split   CLD_flags   CLD_pdc_type CLD_user_data_size CLD_subtitle_file                                                CL
  D_cg_seq_flag CLD_source_format CLD_active_region CLD_user_data                                                                                                                                                                                                 
                                                     FLE_ID      FLE_external_key                                   FLE_sharer_path                                                                                                                  FLE_secondary
  _path                                                                                                               DOM_ID      FLE_file_name                                                    FLE_file_title                                                 
  FLE_file_cats                                                                                                                                                                                 
  
  FLT_ID      SFT_ID      FLE_creator                      FLE_date_created        FLE_date_accessed       FLE_date_modified       FLE_file_deleted FLE_file_dustbinned FLE_keywords                       
                                                                                                                                                                                                                                FLE_timestamp FLE_protected FLE_co
  ntents FLE_write_date          FLE_Synchronised FLE_unique_key                                                                                                                  
  ----------- ---------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ----------- ------------ ------------- -----------
  - ------------------- ---------------- -------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  ------------------------------ ---------- -------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  --------------------------------------------------------------- ---------------------- ----------- ------------------- ------------------ -------------- -------------- -------------- --------------- --------------- ------------- ------------ ----------- --
  ------------ ----------- ----------- -------------- ------------- ----------------------- ----------------------- ----------- -------------- ----------------- ---------------- ----------------- ----------- ----------- ----------- -------- ------------ ----
  ------- --------------- ---------------- ----------- -------------------- -------------- ---------------- -------------- ----------- ----------- ----------- ------------ ------------------ ---------------------------------------------------------------- --
  ------------- ----------------- ----------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  -------------------------------------------------- ----------- -------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- -------------
  ------------------------------------------------------------------------------------------------------------------- ----------- ---------------------------------------------------------------- ---------------------------------------------------------------
  ----------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  ------------------------------------------------------ ----------- ----------- -------------------------------- ----------------------- ----------------------- ----------------------- ---------------- ------------------- -----------------------------------
  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------- ------------- ------
  ------ ----------------------- ---------------- --------------------------------------------------------------------------------------------------------------------------------
  (0 rows affected)*
  </code></pre>