使用 TSQL dbmail 将查询返回到 HTML table 警报电子邮件

Returning query to an HTML table alert email with TSQL dbmail

我有一个非常有用的存储过程,我真的很喜欢它。为了完美,我只需要它做更多的事情。它 return 什么都没有,如果没有满足参数,或者它只是说 "found one",如果 select 与 return 有关系。 现在,粗略的部分。这个数据库有一些有截止日期的档案。当截止日期到来时,我收到一封电子邮件,上面写着 "hey, one of this dossiers has reached the expiration date"。我需要的是从特定档案、编号、参考资料、名称等中获取一些数据... 我怎样才能通过程序达到这个目的,以避免检查所有可能的档案以查看哪个档案已终止。

我无法解决这个问题,抱歉:( 在较早的 sql 服务器版本上有类似的过程,它 return 编辑了该数据,我不记得为此做了什么

CREATE PROCEDURE dbo.DossierEmailSend
 AS
 DECLARE @rows int;
 DECLARE @message varchar(1000);
 SET @rows = (SELECT COUNT(*)  
            FROM bo
            WHERE nmdos LIKE '%preço%'
            AND datafinal = DATEADD(day, -1, CONVERT(date, GETDATE()))
            )
 SET NOCOUNT ON 
 SET @message = '<HTML>As at ' + CONVERT(char(19),GETDATE(),120) + '<BR><BR>ONE UP'

 IF @rows > 0 
 BEGIN

 EXEC dbo.uspSendEmail 'aaaaaaa', 'aaaaaaaa@aaaa', @message, NULL, 'CC EMAIL 1'
 SET NOCOUNT OFF

 END

我刚收到消息说其中一份档案已到截止日期,需要知道是哪一份

如果此代码returns COUNT 档案:

SET @rows = (SELECT COUNT(*)  
        FROM bo
        WHERE nmdos LIKE '%preço%'
        AND datafinal = DATEADD(day, -1, CONVERT(date, GETDATE()))
        )

那就写下所有的档案:

...
DECLARE @DossierNames varchar(MAX);
SELECT @DossierNames = STUFF( (
        SELECT ',' + dossierName
        FROM bo
        WHERE nmdos LIKE '%preço%'
        AND datafinal = DATEADD(day, -1, CONVERT(date, GETDATE()))
        FOR XML PATH('')),
        1, 1,'')

IF @rows IS NOT NULL 
    SET @rows = CONCAT(@rows, ' ', @DossierNames)

...

因为要求将多行传递到电子邮件正文,所以最好以表格格式提供。

这可以通过在电子邮件正文中包含符合条件的 HTML table 档案来实现。

只需将列名替换为来自 table bo.

的正确名称

让我们逐一分解元素:

CREATE PROCEDURE dbo.DossierEmailSend
AS

为符合条件的档案创建馆藏table

DECLARE @dossiers TABLE (col1 varchar(100), col2 varchar(100), col3 varchar(100), col4 varchar(100));

插入符合条件的档案

INSERT INTO @dossiers
SELECT col1, col2, col3, convert(varchar,col4) col4  
    FROM bo
    WHERE nmdos LIKE '%preço%'
    AND datafinal = DATEADD(day, -1, CONVERT(date, GETDATE()))

创建行检查以确定是否发送电子邮件

DECLARE @rows int;
SET @rows = (SELECT COUNT(*) FROM @dossiers)

检查是否有档案符合条件

IF @rows > 0 
BEGIN

设置正文元素

DECLARE @message varchar(1000);
-- declare the xml data to pass to the HTML body
DECLARE @xml NVARCHAR(MAX);
-- body will hold the HTML formatted table in the email
DECLARE @body NVARCHAR(MAX);

创建将每行数据保存为 xml

的列
SET @xml = CAST(( SELECT col1 AS 'td','',col2 AS 'td','', col3 AS 'td','', col4 AS 'td'
FROM @dossiers
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))

为正文设置HTML

SET @body ='<html><body><H3>Dossier Info</H3>
<table border = 1> 
<tr>
<th> col1 </th> <th> col2 </th> <th> col3 </th> <th> col4 </th></tr>'

将所有内容拼接在一起,附加 HTML table

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

SET NOCOUNT ON 

发送电子邮件并将数据table附加到正文

EXEC dbo.uspSendEmail 'Dossiers FOund', 'aaaaaaaa@aaaa', @body, NULL, 'CC EMAIL 1'
SET NOCOUNT OFF

END

完成的解决方案应如下所示:

CREATE PROCEDURE dbo.DossierEmailSend
AS
 --Create a holding table for the dossiers that met the criteria

DECLARE @dossiers TABLE (col1 varchar(100), col2 varchar(100), col3 varchar(100), col4 varchar(100));

 --Insert the dossiers that met the criteria

INSERT INTO @dossiers
SELECT col1, col2, col3, convert(varchar,col4) col4  
    FROM bo
    WHERE nmdos LIKE '%preço%'
    AND datafinal = DATEADD(day, -1, CONVERT(date, GETDATE()))

 --Create a row check to determine whether to send the email or not

DECLARE @rows int;
SET @rows = (SELECT COUNT(*) FROM @dossiers)

 --Check if any dossiers met the criteria

IF @rows > 0 
BEGIN

 --Set the body elements

DECLARE @message varchar(1000);
-- declare the xml data to pass to the HTML body
DECLARE @xml NVARCHAR(MAX);
-- body will hold the HTML formatted table in the email
DECLARE @body NVARCHAR(MAX);

 --Create the columns that will hold each row of data as xml

SET @xml = CAST(( SELECT col1 AS 'td','',col2 AS 'td','', col3 AS 'td','', col4 AS 'td'
FROM @dossiers
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))

 --Set the HTML for the body

SET @body ='<html><body><H3>Dossier Info</H3>
<table border = 1> 
<tr>
<th> col1 </th> <th> col2 </th> <th> col3 </th> <th> col4 </th></tr>'

 --Stitch everything together, appending the HTML table

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

SET NOCOUNT ON 

 --Send the email and append the data table to the body

EXEC dbo.uspSendEmail 'Dossiers FOund', 'aaaaaaaa@aaaa', @body, NULL, 'CC EMAIL 1'
SET NOCOUNT OFF

END