Wednesday, April 24, 2013

How to send Email from SQL Server with attached csv file

Please see the below sample code which will send email which will have an csv attachement.

Exec [msdb].[dbo].[sp_send_dbmail] 
@profile_name = '',
@recipients = 'emailid',
@body = ' Hello Sir,Please find the attached details...',
@body_format ='HTML',
@subject = 'Subject',
   @attach_query_result_as_file = 1, 
   @query_result_header = 1,
   @query_result_no_padding = 1,
   @query = 'SET NOCOUNT ON; SELECT EmployeeName, Address FROM  EmployeeDetails',
   @query_result_separator = ' ',
   @exclude_query_output = 1,
   @query_attachment_filename = 'EmployeeDetails.csv'

landscape paintings nature art landscape art sky art illustration art artwork art human art beautiful art landscape prints nature prints sky prints illustration prints artwork prints human prints beautiful prints beautiful special promotions nature special promotions sky special promotions illustration special promotions artwork special promotions beautiful canvas prints beautiful painting canvas prints beautiful framed prints beautiful painting framed prints beautiful posters beautiful painting posters