SQL触发器只执行一次

时间:2018-05-15 22:51:44

标签: sql-server email triggers

我们正在尝试编写一个在插入/更新时发送电子邮件的触发器。这需要在每次发生变化时运行。当前触发器仅运行一次,然后不再运行。 这是代码:

USE db
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER emailonupdate
ON table
FOR INSERT, UPDATE
AS 
Begin
    SET NOCOUNT ON;
    DECLARE @BODY NVARCHAR(MAX) = N' '; 
    SELECT @BODY += CHAR(13) + CHAR(10) + RTRIM(inserted.col1 + ' has added an item in db to ID# ' + inserted.col2) FROM inserted
    WHERE inserted.col1 IN (1263, 1276, 1277);

IF EXISTS (SELECT 1 FROM inserted WHERE col1 IN (1263, 1276, 1277))
    BEGIN
        EXEC msdb.dbo.sp_send_dbmail
          @profile_name = 'DBA_Notifications',
          @recipients = 'me@myemail.com',
          @subject = 'Database Email', 
          @body = @body;
    END
End

0 个答案:

没有答案