根据条件发送警报

时间:2017-03-21 20:42:12

标签: sql-server tsql

我们运行SQL Server 2012.如果满足条件,我想生成警报电子邮件。这将在SQL Server代理中设置并每小时运行一次。

基本上会运行以下查询:

SELECT 
    IBTRANSACTIONID, SUBCONSTATUS 
FROM 
    PSAPMSGSUBCON 
WHERE
    SUBCONSTATUS = 3  
    AND STATUSSTRING = 'WRKNG'  

IF --(need some input here)
    EXEC msdb.dbo.sp_send_dbmail --(then would call this to issue email)

感谢回复

1 个答案:

答案 0 :(得分:1)

You may try this one.

DECLARE 
    @IBTRANSACTIONID VARCHAR(100) = NULL, 
    @SUBCONSTATUS VARCHAR(100) =NULL
SELECT 
    @IBTRANSACTIONID = IBTRANSACTIONID, 
    @SUBCONSTATUS = SUBCONSTATUS 
FROM 
    PSAPMSGSUBCON 
WHERE
    SUBCONSTATUS = 3  
    AND STATUSSTRING = 'WRKNG'  

IF (@IBTRANSACTIONID IS NOT NULL AND @IBTRANSACTIONID <>'') AND (@SUBCONSTATUS IS NOT NULL OR @IBTRANSACTIONID <>'')
    BEGIN
        EXEC msdb.dbo.sp_send_dbmail --(then would call this to issue email)
    END