question

dolleyes avatar image
dolleyes asked

How to email a sql report

i have the following script that I run to check for any missing database backups. How can I get the output file to be emailed to me? SELECT CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server , master.dbo.sysdatabases.NAME AS database_name , NULL AS [Last Data Backup Date] , 9999 AS [Backup Age (Hours)] FROM master.dbo.sysdatabases LEFT JOIN msdb.dbo.backupset ON master.dbo.sysdatabases.name = msdb.dbo.backupset.database_name WHERE msdb.dbo.backupset.database_name IS NULL AND master.dbo.sysdatabases.name <> 'tempdb' ORDER BY msdb.dbo.backupset.database_name
sql querybackups
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Rizwan avatar image
Rizwan answered
First configure the Database Mail ane below SP as an example. EXEC sp_send_dbmail @profile_name = 'YourProfilename', @recipients = 'YourMailid@domain.com', @subject = 'SubjectLine', @body = 'Give details.', @body_format = 'HTML', @query = 'YourQuery', @attach_query_result_as_file = 1, @query_result_header = 1, @query_result_separator = ' ', @exclude_query_output = 1, @query_result_width = 100, @query_result_no_padding = 1, @query_attachment_filename = 'filename.csv', @file_attachments = 'C: oldername ilename.csv'
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

dolleyes avatar image dolleyes commented ·
Thank you Rizwan for the response. I have database mail configured. Please excuse my lack of understanding, I would appreciate it if you would clarify.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.