views:

18

answers:

1

Hi

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?

+2  A: 

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()
Preet Sangha