A trigger is a special kind of a store procedure
that executes in response to certain action on the table like insertion,
deletion or updating of data. It is a database object which is bound to a table
and is executed automatically. You can’t explicitly invoke triggers. The only
way to do this is by performing the required action on the table that they are
assigned to.
Types of
Triggers
There are three action query types that you use in
SQL which are INSERT, UPDATE and DELETE. So, there are three types of triggers
and hybrids that come from mixing and matching the events and timings that fire
them. Basically, triggers are classified into two main types:
- After Triggers (For Triggers)
- Instead Of Triggers
After
Insert Trigger
This trigger is fired after an INSERT on
the table. Let’s create the trigger as:
CREATE TRIGGER trgAfterInsert ON
[dbo].[Employee_Test]
FOR INSERT
AS
Declare
@empid int;
Declare
@empname varchar (100);
Declare
@empsal decimal (10,2);
Declare
@audit_action varchar (100);
Select
@empid=i.Emp_ID from inserted i;
Select
@empname=i.Emp_Name from inserted i;
Select
@empsal=i.Emp_Sal from inserted i;
Set
@audit_action='Inserted Record -- After Insert Trigger.';
Insert
into Employee_Test_Audit
(Emp_ID, Emp_Name, Emp_Sal, Audit_Action, Audit_Timestamp)
Values
(@empid, @empname, @empsal, @audit_action, getdate ());
PRINT
'AFTER INSERT trigger fired.'
GO
The CREATE TRIGGER statement is used to
create the trigger. THE ON clause specifies the table name on which the trigger
is to be attached. The FOR INSERT specifies that this is an AFTER INSERT
trigger. In place of FOR INSERT, AFTER INSERT can be used. Both of them mean
the same.
In the trigger body, table named inserted has
been used. This table is a logical table and contains the row that has been
inserted. I have selected the fields from the logical inserted table from the
row that has been inserted into different variables, and finally inserted those
values into the Audit table.
AFTER UPDATE Trigger:
This trigger is fired after an update on
the table. Let’s create the trigger as:
CREATE TRIGGER trgAfterUpdate ON
[dbo].[Employee_Test]
FOR UPDATE
AS
Declare
@empid int;
Declare
@empname varchar (100);
Declare
@empsal decimal (10,2);
Declare
@audit_action varchar (100);
Select
@empid=i.Emp_ID from inserted i;
Select
@empname=i.Emp_Name from inserted i;
Select
@empsal=i.Emp_Sal from inserted i;
if
update(Emp_Name)
set
@audit_action='Updated Record -- After Update Trigger.';
if
update(Emp_Sal)
set
@audit_action='Updated Record -- After Update Trigger.';
Insert
into Employee_Test_Audit (Emp_ID, Emp_Name, Emp_Sal, Audit_Action, Audit_Timestamp)
Values
(@empid, @empname, @empsal, @audit_action, getdate ());
PRINT
'AFTER UPDATE Trigger fired.'
GO
The AFTER UPDATE Trigger is created in
which the updated record is inserted into the audit table. There is no logical
table updated like the logical table inserted. We can obtain the updated value
of a field from the update (column_name) function.
In our trigger, we have
used, if update (Emp_Name) to check if the column Emp_Name has been updated. We
have similarly checked the column Emp_Sal for an update.
AFTER
DELETE Trigger:
This trigger is fired after a delete on the table.
Let’s create the trigger as:
CREATE TRIGGER trgAfterDelete ON [dbo].[Employee_Test]
AFTER DELETE
AS
Declare
@empid int;
Declare
@empname varchar (100);
Declare
@empsal decimal (10,2);
Declare
@audit_action varchar (100);
Select
@empid=d.Emp_ID from deleted d;
Select
@empname=d.Emp_Name from deleted d;
Select
@empsal=d.Emp_Sal from deleted d;
Set
@audit_action='Deleted -- After Delete Trigger.';
Insert into
Employee_Test_Audit
(Emp_ID, Emp_Name, Emp_Sal, Audit_Action, Audit_Timestamp)
Values (@empid,
@empname, @empsal, @audit_action, getdate ());
PRINT 'AFTER
DELETE TRIGGER fired.'
GO
In this trigger, the deleted record’s data is picked
from the logical deleted table and inserted into the audit table.
Instead Of Triggers:
These can be used as an interceptor for anything that
anyone tried to do on our table or view.
If you define an Instead Of trigger on
a table for the Delete operation, they try to delete rows, and they will not
actually get deleted (unless you issue another delete instruction from within
the trigger)
INSTEAD OF TRIGGERS can be classified further into
three types as:
INSTEAD OF INSERT Trigger.
INSTEAD OF UPDATE Trigger.
INSTEAD OF DELETE Trigger.
Let’s create an Instead Of Delete Trigger as:
CREATE TRIGGER trgInsteadOfDelete ON
[dbo].[Employee_Test]
INSTEAD OF DELETE
AS
declare
@emp_id int;
declare
@emp_name varchar(100);
declare
@emp_sal int;
select
@emp_id=d.Emp_ID from deleted d;
select
@emp_name=d.Emp_Name from deleted d;
select
@emp_sal=d.Emp_Sal from deleted d;
BEGIN
if(@emp_sal>1200)
begin
RAISERROR('Cannot
delete where salary > 1200',16,1);
ROLLBACK;
end
else
begin
delete
from Employee_Test where Emp_ID=@emp_id;
COMMIT;
insert
into Employee_Test_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
values(@emp_id,@emp_name,@emp_sal,'Deleted
-- Instead Of Delete Trigger.',getdate());
PRINT
'Record Deleted -- Instead Of Delete Trigger.'
end
END
GO
This trigger will prevent the deletion of records from
the table where Emp_Sal > 1200. If such a record is deleted, the Instead Of
Trigger will rollback the transaction, otherwise the transaction will be
committed.
No comments:
Post a Comment