Emailing a Backup Report

I have this query which will email a CSV file of backup info:

EXEC msdb.dbo.sp_send_dbmail
@recipients=N'test@test.com',@body='Message Body',
@subject ='Message Subject',@profile_name ='profile_name',
@query ='SELECT
               CONVERT(CHAR(100), SERVERPROPERTY(''Servername'')) AS Server,
               msdb.dbo.backupset.database_name,
               msdb.dbo.backupset.backup_start_date,
               msdb.dbo.backupset.backup_finish_date,
               msdb.dbo.backupset.expiration_date,
               CASE msdb..backupset.type
                     WHEN ''D'' THEN ''Database''
                     WHEN ''L'' THEN ''Log''
                     WHEN ''I'' THEN ''DIFF''
               END AS backup_type,
               msdb.dbo.backupset.backup_size,
               msdb.dbo.backupmediafamily.logical_device_name,
               msdb.dbo.backupmediafamily.physical_device_name,  
               msdb.dbo.backupset.name AS backupset_name,
               msdb.dbo.backupset.description
            FROM   msdb.dbo.backupmediafamily
               INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
            WHERE  (CONVERT(datetime, msdb.dbo.backupset.backup_start_date, 102) >= GETDATE() - 1)
            ORDER BY
               msdb.dbo.backupset.database_name,
               msdb.dbo.backupset.backup_finish_date',
@query_result_width = 900,
@attach_query_result_as_file = 1,@query_attachment_filename ='Results.csv'

The issue is that the CSV file is not easy to read. Some columns have large gaps and some have no gaps. Also the headers do not line up. How can I produce a nice looking table?
GeodynamicsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel_PLDB Expert/ArchitectCommented:
Have you tried @query_result_no_padding parameter? You also have @query_result_width but you can't use both at once.

EXEC msdb.dbo.sp_send_dbmail
@recipients=N'test@test.com',@body='Message Body',
@subject ='Message Subject',@profile_name ='profile_name',
@query ='SELECT
               CONVERT(CHAR(100), SERVERPROPERTY(''Servername'')) AS Server,
               msdb.dbo.backupset.database_name,
               msdb.dbo.backupset.backup_start_date,
               msdb.dbo.backupset.backup_finish_date,
               msdb.dbo.backupset.expiration_date,
               CASE msdb..backupset.type
                     WHEN ''D'' THEN ''Database''
                     WHEN ''L'' THEN ''Log''
                     WHEN ''I'' THEN ''DIFF''
               END AS backup_type,
               msdb.dbo.backupset.backup_size,
               msdb.dbo.backupmediafamily.logical_device_name,
               msdb.dbo.backupmediafamily.physical_device_name,  
               msdb.dbo.backupset.name AS backupset_name,
               msdb.dbo.backupset.description
            FROM   msdb.dbo.backupmediafamily
               INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
            WHERE  (CONVERT(datetime, msdb.dbo.backupset.backup_start_date, 102) >= GETDATE() - 1)
            ORDER BY
               msdb.dbo.backupset.database_name,
               msdb.dbo.backupset.backup_finish_date',
@query_result_width = 900,
@attach_query_result_as_file = 1,@query_attachment_filename ='Results.csv',@query_result_no_padding=1

Open in new window

0
Alpesh PatelAssistant ConsultantCommented:
Please format Source appropriate for CSV file
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Daniel_PLDB Expert/ArchitectCommented:
PatelAlpesh is right. SQL Server now formats csv file in Unicode so your data lands in one cell in csv file.
You could work this around by creating csv file using e.g. bcp and then send email by sp_send_db_mail attaching csv file created by bcp.
0
GeodynamicsAuthor Commented:
I've built the report in SSRS. Good suggestions though, I shall give you both 125 points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.