DECLARE @Column1Name VARCHAR(255)
DECLARE @Query VARCHAR(2048)
SET @Column1Name = '[sep=,' + CHAR(13) + CHAR(10) + 'Column1]'
SET @Query = 'SELECT Column1 AS ' + @Column1Name + ', Column2, Column3 FROM myTable'
EXEC msdb.dbo.sp_send_dbmail
@profile_name=XXX //this should be created already in SQL
@recipients = ‘umapathi.k@gmail.com’,
@subject= ‘SQL Mail’,
@body= ‘This is the mail generated from SQL,
,@query=@Query
,@attach_query_result_as_file=1
,@query_attachment_filename='Results.csv' // you can set .xlsx or txt as well
,@query_result_separator=',' --enforce csv
,@query_result_no_padding=1 --trim
,@query_result_width=32767 --avoid wordwrap
Note : when you execute this Query, MAIL QUEUED message will be display and wait for
2 mins to receive the mail
No comments:
Post a Comment