我需要在表上創建更新觸發器,如果某些插入值存在,它也會更新表。這可能嗎?我已經嘗試過,當插入的值存在並觸發觸發器時,觸發器會進行更新並再次觸發。最終 - 僵局。有沒有辦法完成這個安全?SQL Server 2008更新觸發器
驗證碼:
CREATE TRIGGER dbo.trg_updateaddress on dbo.Customers
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @original_source varchar(50)
SELECT @original_source = address_source FROM deleted
IF EXISTS (SELECT * FROM inserted i
INNER JOIN deleted d
ON i.cust_id = d.cust_id
WHERE i.address_source IS NOT NULL
AND (i.[address] <> d.[address]
OR i.address2 <> d.address2
OR i.city <> d.city
OR i.[state] <> d.[state]
OR i.zip <> d.zip
OR i.country <> d.country
OR i.phone <> d.phone
OR i.cell <> d.cell
OR i.email <> d.email))
BEGIN
UPDATE customers
SET address_changed = GETDATE()
END
ELSE
BEGIN
UPDATE customers
SET address_source = @original_source
END
END
你能顯示你的觸發代碼? –
上面貼的代碼 –
你是否希望遞歸觸發觸發器,或者這是不必要的? – RBarryYoung