好的,这里更接近肯定,但现在它在一次更新记录上发送了大量的电子邮件?我知道我在这里遗漏了一些东西。第一次使用游标,所以我猜我在那里做错了什么?一如既往,感谢您的帮助!!
create trigger eMailScheduleChange on dbo.BOOKINGS after update as
set nocount on;
Declare EmailCursor Cursor read_only for
select r.Name as RName
, c.Name as CName
, i.BookingTypeId
, i.Start
, i.Finish
, r.Email
from Wallchart.dbo.BOOKINGS b
inner join Wallchart.dbo.CUSTOMERS c on b.CustomerId = c.CustomerId
inner join Wallchart.dbo.RESOURCES r on b.ResourceId = r.ResourceId
inner join Inserted i on i.CustomerId = b.CustomerId
where i.BookingTypeId <> b.BookingTypeId
Declare @Email as varchar(50)
Declare @CName as varchar(100)
Declare @Start as datetime
Declare @RName as varchar (100)
Declare @Finish as datetime
Declare @body as varchar (255)
Declare @BookingTypeId as varchar (50)
open EmailCursor
Fetch next from EmailCursor
INTO @Email, @CName, @Start, @RName, @Finish, @BookingTypeId
While @@FETCH_STATUS=0
BEGIN
Set @body = '<Account cancelled>' + @CName + @Start + @Finish
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQLMail',
@recipients = @Email,
@subject = 'Account Update',
@body = @body
FETCH NEXT FROM EmailCursor INTO @Email, @CName, @Start, @RName, @Finish, @BookingTypeId
SET NOCOUNT OFF
END
Close EmailCursor
Deallocate EmailCursor
答案 0 :(得分:0)
SQL Server不支持从脚本直接邮寄。但你有其他选择。
首先创建一个表来捕获正在更改的记录,并设置一个EmailFlag,以便您可以确定是否已通知该用户该更改。
创建商店程序,该程序将为您提供已经过一些更改但未通过电子邮件通知的所有用户的列表(EmailFlag =“N”或类似内容)
现在使用SSIS创建发送邮件任务,以便为所有这些用户发送详细信息电子邮件。
为每个用户发送电子邮件后,您可以将该电子邮件更新为该记录的“Y”。您可以在SQL Server中使SSIS包成为预定作业,以便它在特定时间间隔内自动运行。
您可以在SSIS中为每个循环容器使用a,以便您可以单独向每个收件人发送邮件
有关详细信息,请参阅以下链接 SSIS - How to configure a send mail task
答案 1 :(得分:0)
我想您可能会觉得触发器中的查询不允许从另一个表中读取数据。情况并非如此,或触发器相当无用。以下是触发器外观的存根。我演示了如何提取您想要的信息。从那里它只是调用sp_send_dbmail。
create trigger MyTrigger on dbo.BOOKINGS for update as
set nocount on;
select r.Name
, c.Name
, i.BookingTypeId
, i.Start
, i.Finish
, r.Email
from Schedule.dbo.BOOKINGS b
inner join Wallchart.dbo.CUSTOMERS c on b.CustomerId = c.CustomerId
inner join Schedule.dbo.RESOURCES r on b.ResourceId = r.ResourceId
inner join Inserted i on i.SomeKeyValue = b.SomeKeyValue
where i.BookingTypeID <> b.BookingTypeID
--Now you can see how to capture all the information. All that is left is to call sp_send_dbmail
--https://msdn.microsoft.com/en-us/library/ms190307.aspx