What is trigger and different types of Triggers?
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 INSERT Trigger.
AFTER UPDATE Trigger.
AFTER DELETE Trigger.
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.
Cust_ID | Cust_Name | Operation_Performed | Date_Time |
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.'
Query:- insert into Customer values ('A-10','Danish')
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.
Let us see the example of “After Update Trigger” for better understanding.
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())
Now, update a record into “Customer” table:-
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.Query:- select * from Customer_Audit
Now for, “After Delete Trigger”:-This trigger fire after a delete operation performed on a 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 INSERT Trigger.
Instead of UPDATE Trigger.
Instead of DELETE Trigger.
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;
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:-
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
0 comments:
Post a Comment