title: sp_send_dbmail executed from job fails with query result attached as file
body: <p>I have faced with the following issue: when trying to send email with results of query attached as file, using <strong>sp_send_dbmail</strong> via executing ordinary query everything seems to be working OK.</p>
  
  <p>But if add the same code into JobStep and run the job, it fails. </p>
  
  <p>Error in job history says  </p>
  
  <blockquote>
    <p><em>Error formatting query, probably invalid parameters [SQLSTATE 42000] (Error 22050).  The step failed.</em></p>
  </blockquote>
  
  <p>But when I comment out parameter that refers to file attaching it starts working correctly again.</p>
  
  <pre><code>exec msdb.dbo.sp_send_dbmail 
      @profile_name = 'profile_name', 
      @recipients  = 'some@mail.com',
      @body = 'body',
      @subject = 'subj',
      --Parameters that refers to attached file
      @attach_query_result_as_file = 1, 
      @query_result_header = 0,
      @query_result_no_padding = 1,
      @query = 'select 1',
      @query_attachment_filename = 'test.csv'
  </code></pre>
  
  <p>Any suggestions?</p>