Dav*_*ard 0 sql t-sql sp-send-dbmail sql-server-2008
我有以下每天在日常工作中运行的 SQL。我希望电子邮件中的结果在查询结果的每一行末尾都有一个换行符。
目前,电子邮件将所有内容都放在同一行,而不是 SQL 查询中的每行一行
EG 如果查询返回 10 行,则应将其放在电子邮件中的 10 行而不是连续的一行
IF (select count(*)
from HSOfficeDocuments h
where h.Expiry_Date <= dateadd(year, 1, getdate())) > 0
begin
exec msdb.dbo.sp_send_dbmail
@profile_name = 'OfficeNotificationsProfile',
@recipients = 'test@test.com',
@subject = 'Expiring Office Documents',
@query = 'select o.office, hs.document_type, h.Expiry_Date
from officesystem.dbo.HSOfficeDocuments h
join officesystem.dbo.HS_Document_Type hs on hs.id = h.Document_Type_id
join officesystem.dbo.Offices o on o.id = h.office_id
where h.Expiry_Date <= dateadd(year, 1, getdate())',
@query_result_header = 0,
@body_format = 'HTML'
end
Run Code Online (Sandbox Code Playgroud)
你可以试试下面的查询。由于邮件正文内容支持 HTML,我们可以使用 HTML 代码来格式化结果。
IF (select COUNT(*) from HSOfficeDocuments h
where h.Expiry_Date <= DATEADD(year, 1, GETDATE())) > 0
begin
exec msdb.dbo.sp_send_dbmail
@profile_name = 'OfficeNotificationsProfile',
@recipients = 'test@test.com',
@subject = 'Expiring Office Documents',
@query = 'select o.office, hs.document_type, h.Expiry_Date,''<br>'' from
officesystem.dbo.HSOfficeDocuments h
join officesystem.dbo.HS_Document_Type hs on hs.id = h.Document_Type_id
join officesystem.dbo.Offices o on o.id = h.office_id
where h.Expiry_Date <= DATEADD(year, 1, GETDATE())',
@query_result_header=0,
@body_format = 'HTML'
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
118 次 |
| 最近记录: |