根据更新的每个特定字段发送电子邮件
Send email based on each specific field that is updated
SQL Server 2017 触发器于 Table
我有以下触发器 运行,它有效。但是,我想根据更新的特定字段发送不同的电子邮件部分。我基本上想使用 SQL 服务器发送邮件代码发送给不同字段的不同用户。我想知道是否有某种方法可以合并该声明?我希望我问的是正确的!我的 SQL 就像 AT&T 的商业广告一样...很好!因此,如果您可以提供帮助,请提供代码,如果这实际上可以完成的话。例如:
Send/do 如果名字已更新:
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Echo System',
@recipients = 'sample1.com',
@subject = 'Send email for sample1',
@body = 'Please start a background check'
Send/do 如果姓氏已更新
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Echo System',
@recipients = 'sample2.com',
@subject = 'Send email for sample2',
@body = 'Please start a background check'
Send/do这是国籍更新。
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
pseudo-table 来处理多行。以及如何检测值的变化。
至于为每个更改类型发送不同的电子邮件,您已经有一个 IF
语句来分配您的 @activity
所以只需扩展它以设置要通过电子邮件发送的值。
但是正如上面所评论的那样,在触发器中发送电子邮件是一个非常糟糕的主意。相反,您应该将要通过电子邮件发送的数据插入队列 table,然后让另一个服务处理该队列。
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
, convert(bit, case when coalesce(I.lastname,'') <> coalesce(D.lastname,'') then 1 else 0 end) lastnameModified
, I.lastname
, convert(bit, 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,'') <> coalesce(D.firstname,'')
or coalesce(I.lastname,'') <> coalesce(D.lastname,'')
or coalesce(I.nationality,'') <> coalesce(D.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'
, @profile_name = 'Echo System'
, @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'
, @profile_name = 'Echo System'
, @recipients = 'sample3.com'
, @subject = 'Send Email for Sample2'
, @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;
SQL Server 2017 触发器于 Table 我有以下触发器 运行,它有效。但是,我想根据更新的特定字段发送不同的电子邮件部分。我基本上想使用 SQL 服务器发送邮件代码发送给不同字段的不同用户。我想知道是否有某种方法可以合并该声明?我希望我问的是正确的!我的 SQL 就像 AT&T 的商业广告一样...很好!因此,如果您可以提供帮助,请提供代码,如果这实际上可以完成的话。例如:
Send/do 如果名字已更新:
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Echo System',
@recipients = 'sample1.com',
@subject = 'Send email for sample1',
@body = 'Please start a background check'
Send/do 如果姓氏已更新
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Echo System',
@recipients = 'sample2.com',
@subject = 'Send email for sample2',
@body = 'Please start a background check'
Send/do这是国籍更新。
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
pseudo-table 来处理多行。以及如何检测值的变化。
至于为每个更改类型发送不同的电子邮件,您已经有一个 IF
语句来分配您的 @activity
所以只需扩展它以设置要通过电子邮件发送的值。
但是正如上面所评论的那样,在触发器中发送电子邮件是一个非常糟糕的主意。相反,您应该将要通过电子邮件发送的数据插入队列 table,然后让另一个服务处理该队列。
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
, convert(bit, case when coalesce(I.lastname,'') <> coalesce(D.lastname,'') then 1 else 0 end) lastnameModified
, I.lastname
, convert(bit, 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,'') <> coalesce(D.firstname,'')
or coalesce(I.lastname,'') <> coalesce(D.lastname,'')
or coalesce(I.nationality,'') <> coalesce(D.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'
, @profile_name = 'Echo System'
, @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'
, @profile_name = 'Echo System'
, @recipients = 'sample3.com'
, @subject = 'Send Email for Sample2'
, @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;