我在 Microsoft SQL Server 中创建了一个更新触发器,如果日期连续更改,它会向我发送电子邮件。
与此类似:
IF UPDATE(ColumnName)
BEGIN
DECLARE @columnVal AS DATETIME
SELECT
@columnVal = i.columnName
FROM
inserted i JOIN deleted d on i.RowId= d.RowId;
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'me',
@recipients = 'me@company.com',
@body = 'blah blah datechange',
@body_format = 'HTML',
@subject = 'subject';
END
有一段时间效果很好。
然后我切换到批量更新,如果多行的日期发生变化,则只发送第一行电子邮件。我试图设置一个光标来滚动更改,但我无法让它工作,类似于下面:
DECLARE @columnVal AS DATETIME
DECLARE cur CURSOR LOCAL READ_ONLY FAST_FORWARD FOR
SELECT
i.ColumnName
FROM
inserted i JOIN deleted d on i.RowId= d.RowId;
OPEN cur
FETCH NEXT FROM cur INTO @columnVal
WHILE @@FETCH_STATUS = 0
BEGIN
IF UPDATE(ColumnName)
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'me',
@recipients = 'me@company.com',
@body = 'blah blah datechange',
@body_format = 'HTML',
@subject = 'subject';
END
FETCH NEXT FROM cur INTO @columnVal
END
CLOSE cur
DEALLOCATE cur
关于如何完成这项任务的任何想法?Update(ColumnName) 函数甚至可以在游标内正常工作(它实际上会告诉我该列是否已针对该行进行了更新吗?)