Types of Triggers: Create INT Identity Varchar Decimal
Types of Triggers: Create INT Identity Varchar Decimal
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:
Lets create After triggers. First of all, lets create a table and insert some sample data. Then, on this
table, I will be attaching several triggers.
I will be creating an AFTER INSERT TRIGGER which will insert the rows inserted into the table into
another audit table. The main purpose of this audit table is to record the changes in the main table.
This can be thought of as a generic audit trigger.
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.
To see the newly created trigger in action, lets insert a row into the main table as:
Now, a record has been inserted into the Employee_Test table. The AFTER INSERT trigger attached
to this table has inserted the record into the Employee_Test_Audit as:
if update(Emp_Name)
set @audit_action='Updated Record -- After Update Trigger.';
if update(Emp_Sal)
set @audit_action='Updated Record -- After Update Trigger.';
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.
In this trigger, the deleted records data is picked from the logical deleted table and inserted into
the audit table. Lets fire a delete on the main table. A record has been inserted into the audit table
as:
All the triggers can be enabled/disabled on the table using the statement
This disables the After Delete Trigger named trgAfterDelete on the specified table.
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. Now, lets try to delete a record with the Emp_Sal >1200 as:
This will print an error message as defined in the RAISE ERROR statement as:
Conclusion
In this article, I took a brief introduction of triggers, explained the various kinds of triggers After
Triggers and Instead Of Triggers along with their variants and explained how each of them works. I
hope you will get a clear understanding about the Triggers in SQL Server and their usage.