我在从SQL服务器发送HTML格式的电子邮件时遇到问题。
使用以下代码部分,我收到"Line 1, incorrect syntax near '<'"
错误。
SET @tableHTML =
'<H1>Progress Report</H1>' +
'<table border="1">' +
'<tr>' +
'<th>Project Name</th>' +
'<th>Platform</th>' +
'<th>Due By</th>' +
'<th>Current Status</th>' +
'<th>Current State</th>' +
'</tr>' +
CAST (
(
SELECT
td = [Project Name], ' ',
td = Platform, ' ',
td = [Due By], ' ',
td = [Current Status], ' ',
td = [Current State], ' '
FROM [dbo].[table_name]
ORDER BY [Current Status] DESC
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
'</table>' ;
我似乎无法将它钉在任何特别的东西上?任何想法?
由于
更新1:
好的我已经在调试会话中运行了代码,并检查了@tableHTML的内容,内容看起来很好,并且填充了我的表中的预期数据。
意味着错误来自其他地方,所以我这次复制了整个查询。
DECLARE @tableHTML NVARCHAR(MAX);
SET @tableHTML =
'<h1>Progress Report</h1>' +
'<table border="1">' +
'<tr>' +
'<th>Project Name</th>' +
'<th>Platform</th>' +
'<th>Due By</th>' +
'<th>Current Status</th>' +
'<th>Current State</th>' +
'</tr>' +
CAST
(
(
SELECT
td = [Project Name], '',
td = Platform, '',
td = [Due By], '',
td = [Current Status], '',
td = [Current State], ''
FROM [dbo].[table_name]
ORDER BY [Current Status] DESC
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
'</table>';
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'db_mail_account',
@recipients = 'example@example.com',
@subject = 'Daily Project Tracking Report',
@query = @tableHTML,
@body_format = 'HTML';
再次感谢。
答案 0 :(得分:0)
看起来您希望@tableHTML
成为电子邮件的正文,但是您将其作为@query
传递,其中必须包含有效的SQL,因此错误。
请尝试使用@body
:
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'db_mail_account',
@recipients = 'example@example.com',
@subject = 'Daily Project Tracking Report',
@body = @tableHTML,
@body_format = 'HTML';