如何在@body 中为SQL 服务器声明一个变量?

How do I declare a variable in @body for SQL Server?

我正在尝试通过 SQL 服务器发送电子邮件。一切正常,但我无法让 @body 接受我声明的变量。

我也尝试调用一个新变量并将其设置为 @body 但我 运行 遇到了同样的问题。

Declare @Body varchar(max),    
      @TableHead varchar(max),
      @TableTail varchar(max),
      @message as varchar(max)     

Set @message=
    (

    DECLARE @CNT as int, @SLS as NVARCHAR(10)
    select  [employeeid], [Sales]
    into #loctempemployee from tblEmployees  

    Set @CNT =  (Select COUNT (Distinct EmployeeID) from #loctempemployee)

    SELECT tr.Principal As [TD], tr.[Company Name] As [TD], ai.[Action Item] As [TD], ai.Owners As [TD], ai.[Due Date] As [TD], ai.Updated As [TD]
    FROM [tblActionItem] ai
    INNER JOIN tblTripReport tr ON ai.TripReportID = tr.tripreportID
    INNER JOIN tblCustomers cu ON cu.CustomerID = tr.[Customer ID]
    INNER JOIN tblEmployees em ON em.EmployeeID = cu.EmployeeID
    WHERE em.Sales = (Select sales from #loctempemployee Where EmployeeID = (Select top 1 EmployeeID from #loctempemployee))
    For XML raw('tr'), Elements

    Delete #loctempemployee Where EmployeeID = (Select top 1 EmployeeID from #loctempemployee) 
    set @CNT = @CNT -1;

    End

    drop table #loctempemployee 
)

Select @Body = (@message)

评论非常正确 - 您不能在对单一数据类型的变量赋值中执行 SQL 的巨大块。

如果您将想要的数据放入#temp table,您可以使用类似下面的内容。

    DECLARE @xml VARCHAR(MAX);
    SET @xml = CAST(( SELECT [col1] AS 'td','',[col2] AS 'td','', [col3] AS 'td'
    FROM #temptable 
    FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))

    SET @body ='<html><body><H3>(Report Title)</H3>
    <table border = 1> 
    <tr>
    <th> Col1 </th> <th> Col2 </th> <th> Col3 </th>'    

    SET @body = @body + @xml + '</table></body></html>'

    -- mail out contents 
    EXEC msdb.dbo.sp_send_dbmail @recipients = @emailaddress,
                                    @body = @body,
                                    @body_format = 'HTML',
                                    @subject = 'SomeReport',
                                    @profile_name = 'SomeProfileName';

如果我没理解错的话,您是在尝试将 SELECT 的结果分配给 @message。您使用的语法不正确。您不能像在 TSQL 中那样在 SET 中 运行 一批语句。只要您的查询生成字符串结果,您就可以分配 @message 查询结果。所以我认为您可能希望将代码更改为类似这样的内容(将其他语句从 SET 语句中拉出)。

Declare @Body varchar(max),    
      @TableHead varchar(max),
      @TableTail varchar(max),
      @message as varchar(max);     


    DECLARE @CNT as int, @SLS as NVARCHAR(10);
    select  [employeeid], [Sales]
    into #loctempemployee from tblEmployees;  

    Set @CNT =  (Select COUNT (Distinct EmployeeID) from #loctempemployee);

Set @message=
    (
    SELECT tr.Principal As [TD], tr.[Company Name] As [TD], ai.[Action Item] As [TD], ai.Owners As [TD], ai.[Due Date] As [TD], ai.Updated As [TD]
    FROM [tblActionItem] ai
    INNER JOIN tblTripReport tr ON ai.TripReportID = tr.tripreportID
    INNER JOIN tblCustomers cu ON cu.CustomerID = tr.[Customer ID]
    INNER JOIN tblEmployees em ON em.EmployeeID = cu.EmployeeID
    WHERE em.Sales = (Select sales from #loctempemployee Where EmployeeID = (Select top 1 EmployeeID from #loctempemployee))
    For XML raw('tr'), Elements
    );


Delete #loctempemployee Where EmployeeID = (Select top 1 EmployeeID from #loctempemployee); 
    set @CNT = @CNT -1;

drop table #loctempemployee; 

Select @Body = (@message);

此外,您是否在未显示的循环中执行此操作?有一个 END 没有相应的 BEGIN,这也可以解释行 set @CNT = @CNT -1;.