Generate query results and attach to email using SQL Server Database Mail

29 August,2014 by Jack Vamvas

Question: How can I generate sql query results and attach to an email? I’d prefer to use the built in SQL Server Database Mail  application

Answer:  To generate query results and attach to an email  is easy through Database Mail.  Use the sp_send_dbmail  arguments  @query and @attach_query_result_as_a_file.

Note: You must use the value 1 , such as @attach_query_result_as_a_file = 1

This example executes a query  based on details in the @query argument and attaches the output  to an email

EXEC msdb.dbo.sp_send_dbmail

@recipients=N'Jack@sqlserver-dba.com',

@body= 'Some text for body',

@subject = 'Some text for subject',

@profile_name = 'MyProfile1',

@query = ‘select name from sys.sysdatabases’,

@attach_query_result_as_file = 1;


Read More

SQL Server – Send email using Powershell - SQL Server DBA

Send email from Powershell with attachment - SQL Server DBA

SQL Server - Sp_send_dbmail and bcc


Author: Jack Vamvas (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on Generate query results and attach to email using SQL Server Database Mail


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer