【问题标题】:sp_send_dbmail Incorrect syntax near '<'sp_send_dbmail '<' 附近的语法不正确
【发布时间】:2015-03-11 10:49:27
【问题描述】:

我在从 SQL Server 发送 HTML 格式的电子邮件时遇到问题。

使用以下代码部分,我收到 "Line 1, incorrect syntax near '&lt;'" 错误。

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';

再次感谢。

【问题讨论】:

  • 没有什么突出的。我建议您一次删除一行并找出导致 hte 错误的原因。
  • 好的,按照建议的操作并没有真正抛出任何明显的问题,所以我决定深入研究并实时观察输出。有关详细信息,请参阅我原始帖子的更新。

标签: html sql sp-send-dbmail


【解决方案1】:

您似乎希望 @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';

【讨论】:

  • 完成并修复,谢谢。现在已经提出的快速跟进问题。如果表的一列中包含空值,则当前该行将移动到左侧,这会破坏表格式(因为不正确的条目列在错误的列标题下)。关于如何防止这种情况发生的任何想法?
  • 对于每个字段,将其包装在 COALESCE() 调用中,例如td = COALESCE([Project Name], ''), '',
  • 如果您对此答案满意,能否将其标记为已接受? stackoverflow.com/help/accepted-answer
猜你喜欢
  • 2019-07-05
  • 2015-12-12
  • 2013-12-16
  • 1970-01-01
  • 2018-08-14
  • 2011-03-11
  • 2014-02-16
  • 2017-01-11
  • 2020-02-06
相关资源
最近更新 更多