插入比较值并发送邮件的触发器

时间:2015-10-08 10:13:25

标签: sql sql-server stored-procedures triggers

目前,我已经测试了这个触发器,它会在表格的每个插页中向我发送一封电子邮件。

CREATE TRIGGER dr_insert ON data_record 
    AFTER INSERT 
AS 

EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'TEST',
        @recipients = 'azfarkashif@hotmail.com',
        @body = 'New data uploaded',
        @subject = 'TriggerTest';

GO

如何自定义此触发器,以便它考虑新插入的行中的列,比较它(例如temp> 70 || temp< 40),如果条件匹配,那么是否向我发送邮件?

1 个答案:

答案 0 :(得分:1)

您需要使用 inserted 表。它在SQL中也称为魔术表。

CREATE TRIGGER dr_insert ON data_record 
    AFTER INSERT 
AS 
BEGIN
    DECLARE @temp INT

    SET @temp = (SELECT column FROM inserted)

    IF @temp > 70 || @temp <40
        BEGIN
            EXEC msdb.dbo.sp_send_dbmail
            @profile_name = 'TEST',
            @recipients = 'azfarkashif@hotmail.com',
            @body = 'New data uploaded',
            @subject = 'TriggerTest';
        END
END
GO