0

好的,所以我有一份工作,其中有 4 个单独的选择查询。

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'Database Email',
    @recipients = 'dbcheck@xxx.com',
    @subject = 'SQL Replication Deletion Check',
    @query = ''SELECT '\\server1\folder1\SQL\check.sql''
SELECT '' ''
SELECT ''1''
SELECT * FROM [repserv].[dbo].[1repdupecheck]
SELECT '' ''
SELECT ''2''
SELECT * FROM [repserv].[dbo].[2repdupecheck]
SELECT '' ''
SELECT ''3''
SELECT * FROM [repserv].[dbo].[3repdupecheck]
SELECT '' ''
SELECT ''4''
SELECT * FROM [repserv].[dbo].[4repdupecheck]',
    @attach_query_result_as_file = 0,
    @query_result_no_padding = 1,
    @query_result_header = 0;

目前它非常像这样输出为文本。

\\server1\folder1\SQL\check.sql

1
1820 '45074',
1854 '45074',
1822 '45488',

2
1819 '45074',
1853 '45074',
1851 '45488',

3
1871 '43971',
1873 '43971',
1875 '44429',

4
1842 '42729',
1857 '42729',
1870 '43971',

但是我希望它采用表格格式,这样我就可以轻松地一次突出显示一列数据。

我知道这可以用 HTML 来完成,这是唯一可能的方法吗,如果是这样,我将如何去做?

4

1 回答 1

1

我通常这样做的方式是将这些结果放入@body参数中,而不是@query参数中。也就是说,我首先获取所有结果并对其进行格式化,然后简单地将其传递给过程,而不是在过程调用时即时执行。

这可能看起来像这样:

DECLARE @EmailBody NVARCHAR(MAX) = 'Some Text Here <table><thead><tr><th>SomeHeaderOne</th><th>SomeHeaderTwo</th></tr></thead><tbody>##TableBody##</tbody>'
DECLARE @EmailTableContent NVARCHAR(MAX)

SELECT @EmailTableContent = COALESCE(@EmailTableContent + '', '')
                        + '<tr>'
                        + '<td style="border-bottom: solid 1px #ddd; padding: 20px;">' + ColumnOne + '</td>'
                        + '<td style="border-bottom: solid 1px #ddd; padding: 20px; text-align: center;">' + ColumnTwo + '</td>'
                        + '</tr>'
FROM TheTable
WHERE ColumnOne = 'Something'

SELECT @EmailBody = REPLACE(@EmailBody, '##TableBody##', @EmailTableContent)

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Database Email',
@recipients = 'dbcheck@xxx.com',
@subject = 'SQL Replication Deletion Check',
@body = @EmailBody,
于 2014-04-03T14:19:55.647 回答