Skip to main content
2 of 2
included another url
John Eisbrener
  • 9.5k
  • 6
  • 30
  • 63

Since no one else has mentioned this as a possibility, you can do this all directly via TSQL commands.

First, you can directly write out to a file via either a linked server (if the file will have a static path) or via OPENROWSET or OPENDATASOURCE functions if the files are more dynamic in nature. Details on how to do this for excel files can be found here: https://support.microsoft.com/en-us/kb/306397

Details for flat files such as text or csvs can be found here: http://blog.waynesheffield.com/wayne/code-library/ad-hoc-querying/ad-hoc-querying-text-files/

Once you have created the file(s) to your liking, you can send an email via sp_send_dbmail, using the @file_attachments parameter to specify the file. Note that by default there's a 1MB file attachment limit, so if these files will be larger, you will want to adjust the mail profile accordingly (e.g. using sysmail_configure_sp passing the MaxFileSize parameter).

I'm not endorsing this approach over any others, but here's another option for you to chew on.

John

John Eisbrener
  • 9.5k
  • 6
  • 30
  • 63