以下是SQL Server 2005更新觸發器。對於isActive標誌更改的email_subscriberList表上的每個更新,我們都會將審計記錄插入到email_Events表中。這對於單個更新工作正常,但批量更新只記錄最後更新的行。如何轉換下面的代碼以更新每行的插入內容?用於批量更新的SQL Server更新觸發器
CREATE TRIGGER [dbo].[Email_SubscriberList_UpdateEmailEventsForUpdate_TRG]
ON [dbo].[Email_subscriberList]
FOR UPDATE
AS
DECLARE @CustomerId int
DECLARE @internalId int
DECLARE @oldIsActive bit
DECLARE @newIsActive bit
DECLARE @email_address varchar(255)
DECLARE @mailinglist_name varchar(255)
DECLARE @email_event_type varchar(1)
SELECT @oldIsActive = isActive from Deleted
SELECT @newIsActive = isActive from Inserted
IF @oldIsActive <> @newIsActive
BEGIN
IF @newIsActive = 1
BEGIN
SELECT @email_event_type = 'S'
END
ELSE
BEGIN
SELECT @email_event_type = 'U'
END
SELECT @CustomerId = customerid from Inserted
SELECT @internalId = internalId from Inserted
SELECT @email_address = (select email from customer where customerid = @CustomerId)
SELECT @mailinglist_name = (select listDescription from Email_lists where internalId = @internalId)
INSERT INTO Email_Events
(mailshot_id, date, email_address, email_event_type, mailinglist_name)
VALUES
(@internalId, getDate(), @email_address, @email_event_type,@mailinglist_name)
END
你是如何進行批量更新的? – Rup 2010-07-02 14:52:00
多個應用程序將更新此表。 – 2010-07-02 14:54:34