.NET and SQL Server Interview Question - What is trigger and different types of Triggers?
Shivprasad Koirala
Trigger is a SQL server code, which execute when a kind of action on a table occurs like insert, update and delete. It is a database object which is bound to a table and execute automatically.
Triggers are basically of two type’s namely "After Triggers" and "Instead of Triggers". 1.After Triggers:- this trigger occurs after when an insert, update and delete operation has been performed on a table.
“After Triggers” are further divided into three types AFTER INSERT Trigger. AFTER UPDATE Trigger. AFTER DELETE Trigger.
Let us consider that we have the following two tables.
Create “Customer” table with the following field as you see in the below table.
Create “Customer_Audit” table with the following field as you see in the below table.
The main purpose of creating “Customer_Audit” table is to record the data which occurs on the “Customer” table with their respective operation and date-time.
Let’s begin with “After Insert Trigger”:- This trigger fire after an insert operation performed on a table. Let us see the example of “After Insert Trigger” for better understanding.
Query:- Create Trigger TrigInsert on Customer For insert as declare @Cust_ID int; declare @Cust_Name varchar(100); declare @Operation_Performed varchar(100); select @Cust_ID=i.Cust_ID from inserted i; select @Cust_Name=i.Cust_Name from inserted i; set @Operation_Performed='Inserted Record -- After Insert Trigger'; insert into Customer_Audit (Cust_ID,Cust_Name,Operation_Performed,Date_Time) values(@Cust_ID,@Cust_Name,@Operation_Performed,getdate()); PRINT 'AFTER INSERT trigger fired.'
Now, insert a record into Customer table:
Query:- insert into Customer values ('A-10','Danish')
Once the insert statement is successfully done, the record is inserted into the “Customer” table and the “After Trigger” (TrigInsert) is fired and the same record is also stored into “Cutomer_Audit” table.
To see the record in “Customer_Audit” table write query as below:-
Query:- select * from Customer_Audit
You can see that the same record is seen in the “Customer_Audit” table with Operation_performed and the date_time when it was updated.
Now let’s see for “After Update Trigger”:-This trigger fire after an update operation performed on a table. Let us see the example of “After Update Trigger” for better understanding.
Query:- Create trigger TrigUpdate on Customer For Update as declare @Cust_ID int; declare @Cust_Name varchar(100); declare @Operation_Performed varchar(100); select @Cust_ID=i.Cust_ID from inserted i; select @Cust_Name=i.Cust_Name from inserted i; set @Operation_performed='Inserted Record -- After Insert';
if update(Cust_Name) set @Operation_Performed='Updated Record -- After Update Trigger.'; insert into Customer_Audit (Cust_ID,Cust_Name,Operation_Performed,Date_Time) values(@Cust_ID,@Cust_Name,@Operation_Performed,getdate())
PRINT 'AFTER UPDATE Trigger fired.'
Now, update a record into “Customer” table:-
Query:- update Customer set Cust_Name = 'Khan Wasim' where Cust_Code like 'A-16'
The record is updated into the Customer table and the TrigUpdate is fired and it stores a record into “Cutomer_audit” table.
To see the record Customer_Audit table write query for that.
Now for, “After Delete Trigger”:-This trigger fire after a delete operation performed on a table.
In a similar way, you can code “After Delete trigger” on the table.
2.Instead of Triggers:- this trigger fire before the DML operations occur, first inserted and deleted get flourished and then trigger fires
“Instead of Triggers” are further divided into three types Instead of INSERT Trigger. Instead of UPDATE Trigger. Instead of DELETE Trigger.
Let us see the example of “Instead of UPDATE Trigger” for better understanding.
Query:- CREATE TRIGGER trgInsteadOfUpdate ON Customer INSTEAD OF update AS declare @cust_id int; declare @cust_name varchar(100); declare @cust_salary int; select @cust_id=d. Cust_ID from deleted d; select @cust_name=d. Cust_Name from deleted d; select @cust_salary =d.Cust_Salary from deleted d;
BEGIN if(@cust_salary >4500) begin RAISERROR('Cannot delete where salary > 4500',16,1); ROLLBACK; end else begin delete from Customer where Cust_ID =@cust_id; COMMIT; insert into Customer_Audit(Cust_ID,Cust_Name,Cust_Salary,Operation_Performed,Date_Time) values(@cust_id,@cust_name,@cust_salary,'Updated -- Instead Of Updated Trigger.',getdate()); PRINT 'Record Updated -- Instead Of Updated Trigger.' end END Now, update a record into “Customer” table:-
Query:- update Customer set Cust_Name = 'Khan Wasim' where Cust_Code like 'A-09'
When you try to update Customer table it will raise an error as we have use Instead of Update trigger.
Error:- Server: Msg 50000, Level 16, State 1, Procedure trgInsteadOfUpdate, Line 15 Cannot update where salary > 4500
In a similar way, you can code “Instead Delete trigger” and “Instead Insert trigger” on the table.
Regards,
Please click here to see more .NET and SQL Server interview questions