SQL Server : HTML Format in sp_send_dbmail



我使用此查询发送每日报告。我应该对代码进行哪些更改才能获得 html 格式的输出?

EXEC msdb.dbo.sp_send_dbmail
     @profile_name = 'DBMAIL',
     @recipients = 'aa@xyz.com',
     @query = 'Select Country, CompanyName, round(sum(TotalSale), 0) as Sale, count(*) as DownloadedCount from PW.dbo.SalesReport group by Country, CompanyName order by Country',
     @subject = 'Daily Sales Report' ;
EXEC msdb.dbo.sp_send_dbmail
     @profile_name = 'DBMAIL',
     @recipients = 'aa@xyz.com',
     @query = 'Select Country,ltrim(rtrim([Store Name])) as Store, round(sum(TotalSale), 0) as Sale, space(3), max(Convert(varchar(10), LastTime, 108)) as [Last update Time] from PW.dbo.SalesReport where len(Country) > 1 group by Country, [Store Name] order by Country, [Store Name]',
     @subject = 'Daily Sales Detail'

如果您只是快速查阅有关sp_send_dbmail的官方MSDN文档 - 您将很容易看到以下内容:

[ @body_format= ] 'body_format'

邮件正文的格式。参数的类型为 varchar(20),默认值为 NULL。指定后,将设置传出邮件的标头以指示邮件正文具有指定的格式。该参数可能包含以下值之一:

发短信
.HTML

默认为文本。

所以只需添加

@body_format = 'HTML'

到您的电话中,您应该获得所需的HTML格式。