Email contents of table

  • Hi everyone.

    Hope someone can offers some guidance.

    Have a requirement to send the contents of a table via email to a location on a set schedule.

    What is best way to approach this ?

    If anyone has any recommendations on the best way to set this up , please let me know.

    For example should I copy contents to a separate table, or extract from current table and email from there ?

    thank you.

  • Take a look at the "sp_send_dbmail" stored procedure and make use of the @query parameter.

    http://technet.microsoft.com/en-us/library/ms190307(v=sql.100).aspx

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • To sent the results on a schedule, you can create a SQL Agent job and execute the sp_send_dbmail from a job step.

    Most of the time it is no problem to query the table directly. To put the results in a temporary table first would be a waist of resources.

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • Perfect thank you.

    I have the mail profile etc all working.

    Do you know how I would generate the output as a .csv and not a .txt ?

  • Use the result_seperator option:

    exec sp_send_dbmail

    ...

    @query_attachment_filename = 'sample.csv',

    @query_result_separator = ',',

    ...

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • Once again , perfect thank you.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply