AS DBAs we often need to develop DML triggers to capture multiple entries which are either INSERTED, UPDATED or DELETED from a table. A few months back, I came across a situation where the Application Development Team asked me to write a logic  to notify the DBA  if any records were either INSERTED,UPDATED or DELETED from a particular table. The requirement was  that the trigger should capture multiple entries instead of just one single entry. Based on the requirements from the team, I finally decided to implement the required logic.

Whenever we talk about DML triggers,  we often forget the concept of MAGIC tables, i.e. INSERTED and DELETED tables. A DML TRIGGER cannot be written without using the MAGIC tables. Let’s take a a look at exactly what the MAGIC tables holds.

  ACTIVITY                   INSERTED                 DELETED
     DATA IS INSERTED      Holds The New Record.
     DATA IS UPDATED      Holds The New Record.  Holds The Old Record.
     DATA IS DELETED Holds The Deleted Records.

From the above table, we can see that whenever a new record is INSERTED into the main table. Whenever the record gets updated, the INSERTED table holds the new record whereas the DELETED table holds the old record. Whenever the record is deleted from the table, the DELETED table holds the deleted records. The INSERTED and DELETED table gets created only after DML operations i.e. INSERT, UPDATE or DELETE operations are performed on a table. The INSERTED and DELETED tables are an exact replica of the main table (i.e. the table on which the trigger is to be created).

In order to understand DML Triggers, let us consider a scenario where we have a table named student which has two columns named student_id and student_name. We need to notify whenever a new student is inserted, an existing student is updated or deleted from the table.

Create a table named student:

Logic to capture newly inserted records:

First of all, we need to check which new records are inserted into the table:

IF EXISTS(SELECT INS.STUDENT_NAME FROM INSERTED INS

INNER JOIN STUDENT S ON INS.STUDENT_NAME=S.STUDENT_NAME)

This is achieved by performing an INNER JOIN with the INSERTED and Student table. We all know that whenever a new record is inserted into the table, the INSERTED table holds the same, so if we perform a join with the INSERTED and the Student table, we will be able to capture the newly inserted records.

Create a temporary table named #inserted which holds the newly inserted rows:

This also contains a column named row_no, which we will be using to fetch the records row by row.

Newly inserted records are then placed in the temporary tables named #inserted:

1_SQL_Server_Using_DML_Triggers_to_Capture_Multiple_Events

Logic to capture updated records:

First, we check whether any records are updated in the table:

Next we create a temporary table named #updated which holds the updated rows:

create table #updated

(

row_no int,

id int,

name varchar(20)

)

It also contains a column named row_no, which we will be using to fetch the records row by row.

Updated records are then placed in the temporary tables named #updated:

When existing records are updated in the table, we get a notification message as shown in the screen capture below:

2_SQL_Server_Using_DML_Triggers_to_Capture_Multiple_Events

Logic to capture deleted records

First of all, we check whether any records are deleted from the table:

It also contains a column named row_no, which we will be using to fetch the records row by row.

Deleted records are then placed in the temporary tables named #deleted:

Now, whenever any record is deleted from a table, we get notification message as shown in the screen capture below:

3_SQL_Server_Using_DML_Triggers_to_Capture_Multiple_Events

Conclusion:

Thus we have seen that using DML Triggers, we can capture entries which are either INSERTED, UPDATED or DELETED from the table. Future enhancements could be modifying the above logic to notify users via email for any DML activity done on the table.

Many Thanks to all the viewers for giving their valuable time in reading the article. Sincere Thanks to the editor Amit Bansal and all the members of the Community because of whom i get an oppurtunity to learn more and more and excel in my career.

 

Regards

Satnam Singh

Like us on FaceBook Follow us on Twitter

Follow me on FaceBook| Join the fastest growing SQL Server group on FaceBook