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 not what I want. I need to fire before I delete a record. Any ideas?
In this syntax: First, specify the name of the trigger which you want to create after the CREATE TRIGGER keywords. Second, use BEFORE DELETE clause to specify that the trigger is invoked right before a delete event. Third, specify the name of the table that the trigger is associated with after the ON keyword.
First, specify the name of the trigger that you want to create in the CREATE TRIGGER clause. Second, use AFTER DELETE clause to specify the time to invoke the trigger. Third, specify the name of the table, which the trigger is associated with, after the ON keyword.
INSTEAD OF DELETE TRIGGERS are used, to delete records from a view. Introduction. INSTEAD OF DELETE triggers are used to delete records from a View that is based on multiple tables. Description. An INSTEAD OF DELETE trigger gets executed in place of the DELETE event on a table or a View.
Deleting a table automatically deletes any triggers on the table. A trigger is enabled by default when it is created. Disabling a trigger does not drop it.
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.
You can't. But you can perform a rollback in an AFTER DELETE trigger.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With