Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a before delete trigger in SQL Server?

I want to create a before delete trigger. When I delete a record from a table that record has to be inserted into a history table. How can I do this in SQL Server?

like image 228
user1374263 Avatar asked May 04 '12 06:05

user1374263


People also ask

What is the before delete trigger in SQL?

Description. A BEFORE DELETE Trigger means that Oracle will fire this trigger before the DELETE operation is executed.

How do you create a trigger to delete in SQL?

Expand the database that you want, expand Tables, and then expand the table that contains the trigger that you want to delete. Expand Triggers, right-click the trigger to delete, and then click Delete. In the Delete Object dialog box, verify the trigger to delete, and then click OK.

What is instead of delete trigger?

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.

Can we use trigger old in before delete?

trigger. old is available only on the update and delete events.


3 Answers

In this situation, you're probably better off doing a regular "after" trigger. This is the most common approach to this type of situation.

Something like

CREATE TRIGGER TRG_AUD_DEL
ON yourTable
FOR DELETE
AS
     INSERT INTO my_audit_table  (col1, col2, ...)
     SELECT col1, col2...
     FROM DELETED 

What will happen is, when a record (or records!) are deleted from your table, the deleted row will be inserted into my_audit_table The DELETED table is a virtual table that contains the record(s) as they were immediately prior to the delete.

Also, note that the trigger runs as part of the implicit transaction on the delete statement, so if your delete fails and rolls back, the trigger will also rollback.

like image 106
Code Magician Avatar answered Oct 07 '22 18:10

Code Magician


You could also use INSTEAD OF DELETE

CREATE TRIGGER dbo.SomeTableYouWhatToDeleteFrom
ON dbo.YourTable
INSTEAD OF DELETE
AS
BEGIN

     -- Some code you want to do before delete

     DELETE YourTable
     FROM DELETED D
     INNER JOIN dbo.YourTable T ON T.PK_1 = D.PK_1
END
like image 45
Arion Avatar answered Oct 07 '22 18:10

Arion


It could be done in following steps for let’s say in this example I am using customer table:

CREATE TABLE CUSTOMERS(
   ID   INT              NOT NULL,
   NAME VARCHAR (20)     NOT NULL,
   AGE  INT              NOT NULL,
   ADDRESS  CHAR (25) ,
   LAST_UPDATED DATETIME,
   PRIMARY KEY (ID)
);
  1. Create History:

    CREATE TABLE CUSTOMERS_HIST( 
    ID   INT              NOT NULL,
    NAME VARCHAR (20)     NOT NULL,
    AGE  INT              NOT NULL,
    ADDRESS  CHAR (25) ,
    LAST_UPDATED DATETIME,
    PRIMARY KEY (ID)
    );
    
  2. Trigger on source table like below on delete event:

    CREATE TRIGGER TRG_CUSTOMERS_DEL 
    ON CUSTOMERS
    FOR DELETE
    AS
         INSERT INTO CUSTOMERS_HIST (ID, NAME, AGE, ADDRESS, LAST_UPDATED)
         SELECT ID, NAME, AGE, ADDRESS, LAST_UPDATED
         FROM DELETED
    
like image 1
Deepesh Tiwari Avatar answered Oct 07 '22 19:10

Deepesh Tiwari