根据已更新的每个特定字段发送电子邮件

问题描述

表上的sql Server 2017触发器 我正在运行以下触发器,并且可以正常工作。但是,我想根据要更新的特定字段发送不同的电子邮件部分。我基本上想使用sql Server发送邮件代码将不同的字段发送给其他用户。我想知道是否有某种方式可以合并该语句?希望我能正确回答这个问题!我的sql就像AT&T商业广告一样...好吧!因此,如果您可以提供帮助,请提供代码(如果实际上可以做到)。例如:

如果姓氏已更新,则发送/执行此操作:

      EXEC msdb.dbo.sp_send_dbmail
      @profile_name = 'Echo System',@recipients = 'sample1.com',@subject = 'Send email for sample1',@body = 'Please start a background check'

如果姓氏已更新,请发送/执行此操作

      EXEC msdb.dbo.sp_send_dbmail
      @profile_name = 'Echo System',@recipients = 'sample2.com',@subject = 'Send email for sample2',@body = 'Please start a background check'

发送/执行此国籍更新操作。

      EXEC msdb.dbo.sp_send_dbmail
      @profile_name = 'Echo System',@recipients = 'sample3.com',@subject = 'Send email for sample3',@body = 'Please start a background check'
ALTER trigger [dbo].[updatePerson] on
[dbo].[person]
for update
as
 
      declare @personId int;
      declare @firstname varchar(50);
      declare @lastname varchar(50);
      declare @nationality varchar(100);
      declare @activity varchar(100);
 
      select @personId = s.personId from inserted s;
      select @firstname = s.firstname from inserted s;
      select @lastname = s.lastname from inserted s;
      select @nationality = s.nationality from inserted s;
 
      if update(firstname)
                  set @activity = 'Updated person firstname'
      if update(lastname)
                  set @activity = 'Updated person lastname'
      if update(nationality)
                  set @activity = 'Updated person nationality'
 
      
 
      EXEC msdb.dbo.sp_send_dbmail
      @profile_name = 'Echo System',@recipients = 'sampletriggernamen@gmail.com',@subject = ' follow the sampleaction you need to take. http://test.aspx',@body = 'Please start a background check'

解决方法

以下代码显示如何通过遍历Inserted伪表来处理多行。以及如何检测值的变化。

就每种变更类型发送不同的电子邮件而言,您已经有一个IF语句来分配您的@activity,因此只需展开它以设置要通过电子邮件发送的值即可。

但是如上所述,在触发器中发送电子邮件是一个非常糟糕的主意。相反,您应该将要通过电子邮件发送的数据插入队列表,然后让另一个服务处理该队列。

ALTER trigger [dbo].[updatePerson] on
[dbo].[person]
for update
as
begin
    set nocount on;
     
    declare @personId int,@firstname varchar(50),@lastname varchar(50),@nationality varchar(100),@activity varchar(100),@firstNameModified bit,@lastNameModified bit,@nationalityModified bit,@profile_name sysname,@recipients varchar(max),@subject nvarchar(255),@body nvarchar(max);

    select
        I.personId,convert(bit,case when coalesce(I.firstname,'') <> coalesce(D.firstname,'') then 1 else 0 end) firstnameModified,I.firstname,case when coalesce(I.lastname,'') <> coalesce(D.lastname,'') then 1 else 0 end) lastnameModified,I.lastname,case when coalesce(I.nationality,'') <> coalesce(D.nationality,'') then 1 else 0 end) nationalityModified,I.nationality
    into #updatePerson_temp
    from Inserted I
    -- Because its an 'update' trigger we can inner join
    inner join Deleted D on D.personId = I.personId
    where coalesce(I.firstname,'')
    or coalesce(I.lastname,'')
    or coalesce(I.nationality,'');

    while exists (select 1 from #updatePerson_temp) begin
        -- Get first record to handle
        select top 1 @personId = personId,@firstnameModified = firstnameModified,@firstname = firstname,@lastnameModified = firstnameModified,@lastname = lastname,@nationalityModified = nationalityModified,@nationality = nationality
        from #updatePerson_temp;

        -- Following assumes only one change per record,modify to suit
        if @firstnameModified = 1 begin
            select @activity = 'Updated person firstname',@profile_name = 'Echo System',@recipients = 'sample1.com',@subject = 'Send Email for Sample1',@body = 'Please start a background check';
        end; else if @lastnameModified = 1 begin
            select @activity = 'Updated person lastname',@recipients = 'sample2.com',@subject = 'Send Email for Sample2',@body = 'Please start a background check';
        end; else if @nationalityModified = 1 begin
            select @activity = 'Updated person nationality',@recipients = 'sample3.com',@body = 'Please start a background check';
        end;

        -- Instead of sending the email here,queue it for later
        exec msdb.dbo.sp_send_dbmail
            @profile_name = @profile_name,@recipients = @recipients,@subject = @subject,@body = @body;

        -- Delete handled record
        delete from #updatePerson_temp where personId = @personId;
    end;
end;