开发者

How to get value of deleted test in trigger?

开发者 https://www.devze.com 2023-01-18 03:39 出处:网络
I need to put a trigger on Delete on Table1. On delete of record from Table1, I need to update Table2 in trigger, but i need the value of deleted record in the trigger.

I need to put a trigger on Delete on Table1. On delete of record from Table1, I need to update Table2 in trigger, but i need the value of deleted record in the trigger. Example:-

IF OBJECT_ID ('UpdateLog','TR') IS NOT NULL
    DROP TRIGGER UpdateLog;
GO
CREATE开发者_高级运维 TRIGGER UpdateLog
ON Table_1
AFTER DELETE 
AS
   UPDATE Table_2
    SET Date1 = getdate()
    WHERE (UID from deleted record from Table1)
GO

So I need value of deleted record from table1 to update the table2. How?


It should be in the 'deleted' table available in the trigger. See using the inserted and deleted tables

select * from deleted

Note that if you run delete mutiple records can be deleted and your trigger should take into account that the 'deleted' table contains more than one row.

Something along the lines of :

UPDATE t
from table_2 t
inner join deleted d on d.UID = t.UID
    SET t.Date1 = getdate()
0

精彩评论

暂无评论...
验证码 换一张
取 消