我正在从 xp_sendmail 切换一些代码,但没有从 sp_send_dbmail 获得我期望的结果。我正在尝试的查询如下,电子邮件中的消息正文仅包含一行:
我的第一个标题条目
Microsoft 是否真的将其设计为仅返回单行查询的答案(例如 COUNT(*) 或 TOP 1)?有没有办法将整组行作为文本?
DECLARE @tLog TABLE (Sequence INT IDENTITY(1,1), [Rank] TINYINT DEFAULT 1, Line VARCHAR(8000))
INSERT INTO @tLog ([Line]) VALUES ('My first log entry')
INSERT INTO @tLog ([Line]) VALUES ('My first second entry')
/* ... processing ... */
INSERT INTO @tLog ([Rank], [Line]) VALUES (0, 'My first header entry')
/* Global Temp for visibility in sp_send_dbmail */
SELECT Sequence, [Rank], Line
INTO ##tResult1
FROM @tLog
ORDER BY [Rank], Sequence
/* to validate table content only */
SELECT Sequence, [Rank], Line
FROM ##tResult1
ORDER BY [Rank], Sequence
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'MyProfile',
@recipients='me@myaddress.net',
@subject = 'test',
@body_format = 'text',
@body = '',
@query_result_header = 0,
@query_no_truncate = 1,
@query = 'SELECT Line FROM ##tResult1 ORDER BY [Rank], Sequence'
DROP TABLE ##tResult1