每日html电子邮件报告,tsql问题

时间:2018-03-13 15:19:37

标签: sql-server sql-server-2014

我们生成了每日报告并从表中获取信息,但发送的电子邮件未显示“Results2”,但如果我只询问Select查询则可以正常工作。 这是脚本:

DECLARE @tableHTML  NVARCHAR(MAX) ;
DECLARE @tableHTML1  NVARCHAR(MAX) ;



SET @tableHTML1 =
    N'<H2>DailyCheck</H2>' +
    N'<table border="1">' +
    N'<tr><th>Results1</th><th>Results2</th></tr>' +
                           ISNULL(CAST ( (                          
           SELECT  *    
FROM        
(        
   SELECT    
       a.ToDevice, count(distinct p.Email) as 'result'
   FROM    
       Database..AccountTransfer a (nolock)
       join Database..Person p (nolock) on p.VTKId = a.VTkId
   GROUP BY    
       a.ToDevice
) as innerTable    
WHERE        
   result >= 4    
ORDER BY        
   result desc
     FOR XML PATH('tr'), TYPE 
    ) AS NVARCHAR(MAX)), 'No result found') +
    N'</table>' ;


    SET @tableHTML = N'<H1>Daily Check ' + CONVERT(VARCHAR(20), GETDATE() ,120)  + '</h1>' + @tableHTML1 + '</br>'

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Email',
@recipients = 'reciever@email.net',
@subject = 'Daily check query',
@body = @tableHTML,
@body_format = 'HTML'

这是我们在发送的电子邮件中得到的结果:(results2没有显示任何内容)

        DailyCheck
    Results1    Results2
    DFFBB78B-811C-481E-BA33-85B7512960E1114 
    F6ACB39D-7D91-48EE-B2A0-324B48DB26E847  

如果我们只运行

SELECT  * FROM        
    (        
       SELECT    
           a.ToDevice, count(distinct p.Email) as 'result'
       FROM    
           Database..AccountTransfer a (nolock)
           join Database..Person p (nolock) on p.VTKId = a.VTkId
       GROUP BY    
           a.ToDevice
    ) as innerTable    
    WHERE        
       result >= 4    
    ORDER BY        
       result desc

然后我们得到这个结果:(结果2显示结果)

    Results1    Results2
DFFBB78B-811C-481E-BA33-85B7512960E1    114
F6ACB39D-7D91-48EE-B2A0-324B48DB26E8    47

任何人都有任何想法可能出错?有什么代码吗?

这里只有HTML代码:

DECLARE @tableHTML  NVARCHAR(MAX) ;
DECLARE @tableHTML1  NVARCHAR(MAX) ;



SET @tableHTML1 =
    N'<H2>DailyCheck</H2>' +
    N'<table border="1">' +
    N'<tr><th>Results1</th><th>Results2</th></tr>' +

     FOR XML PATH('tr'), TYPE 
    ) AS NVARCHAR(MAX)), 'No result found') +
    N'</table>' ;


    SET @tableHTML = N'<H1>Daily Check ' + CONVERT(VARCHAR(20), GETDATE() ,120)  + '</h1>' + @tableHTML1 + '</br>'

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Email',
@recipients = 'reciever@email.net',
@subject = 'Daily check query',
@body = @tableHTML,
@body_format = 'HTML'

1 个答案:

答案 0 :(得分:0)

感谢帮助,我能够理解它是如何运作得更好的。我做的是编辑这部分:

   SELECT    
       a.ToDevice AS TD, count(distinct p.Email) as 'result'
   FROM    

这为我解决了,感谢所有帮助过的人!