A trigger is a block of code that constitutes a set of T-SQL statements activated in response to certain action, such as insert or delete.

Triggers are used to ensure data integrity before or after performing data manipulations.

Types of Triggers in SQL Server

In SQL Server there are various kinds of triggers can be use for different types of data manipulation operations. In sql Server there are two types of triggers:

DML Triggers

DDL Triggers

For example I crate a trigger on employee table of the kvch database.

  1. Create taigger {trgdepartement} on Hr. Department AFTER UPDATE AS
  2. BEGIN
  3. UPDATE Hr.Department set HR. Department.ModifiedDate=getdate() from Inserted where
  4. Inserted.DepartementID=Hr. Department.DepartmentID;
  5. END;
In the above example statement create trigger named trgDepartment that is fired on every successfully executed update statement on the Hr.Department table. The Trigger updates the ModifiedDate column of every updated value with the current date.