Skip to main content
included another url
Source Link
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][1]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/][2]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][3]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_spsysmail_configure_sp passing the MaxFileSize parameter).

I'm not endorsing this approach over any others, but life is about choices and choices require options, so here's another option for you to chew on.

John [1]: https://support.microsoft.com/en-us/kb/306397 [2]: http://blog.waynesheffield.com/wayne/code-library/ad-hoc-querying/ad-hoc-querying-text-files/ [3]: https://msdn.microsoft.com/en-us/library/ms190307.aspx

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][1]

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/][2]

Once you have created the file(s) to your liking, you can send an email via [sp_send_dbmail][3], 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 life is about choices and choices require options, so here's another option for you.

John [1]: https://support.microsoft.com/en-us/kb/306397 [2]: http://blog.waynesheffield.com/wayne/code-library/ad-hoc-querying/ad-hoc-querying-text-files/ [3]: https://msdn.microsoft.com/en-us/library/ms190307.aspx

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

Source Link
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][1]

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/][2]

Once you have created the file(s) to your liking, you can send an email via [sp_send_dbmail][3], 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 life is about choices and choices require options, so here's another option for you.

John [1]: https://support.microsoft.com/en-us/kb/306397 [2]: http://blog.waynesheffield.com/wayne/code-library/ad-hoc-querying/ad-hoc-querying-text-files/ [3]: https://msdn.microsoft.com/en-us/library/ms190307.aspx