How can I fire a trigger BEFORE a delete in T-SQL 2005?

前端 未结 3 578
别那么骄傲
别那么骄傲 2021-01-01 17:01

How can I fire a trigger BEFORE a delete in T-SQL 2005? The FOR actually fires AFTER an event and they seems no BEFORE argument in the TRIGGER function. The INSTEAD OF is no

3条回答
  •  旧巷少年郎
    2021-01-01 18:01

    You can use the INSTEAD OF option, just explicitly delete the rows at the end. For example:

    CREATE TRIGGER dbo.My_Table_Delete_Instead_Of_Trigger
    ON dbo.My_Table
    INSTEAD OF DELETE
    AS
    BEGIN
    
         -- Do some stuff here
    
         DELETE T
         FROM DELETED D
         INNER JOIN dbo.My_Table T ON T.PK_1 = D.PK_1 AND T.PK_2 = D.PK_2
    END
    

    This assumed a primary key made up of columns PK_1 and PK_2.

提交回复
热议问题