Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What are triggers in Microsoft SQL Server? Kindly Explain with examples.

user-image
Question added by Salman Khalid , Application & API Support Engineer , Adamjee Insurance Co. Ltd.
Date Posted: 2016/12/28
shakeel mir
by shakeel mir , Senior Software Developer , Rawat-Al-Makan

A trigger is a special kind of stored procedure that automatically executes when an event occurs in the database server. DML triggers execute when a user tries to modify data through a data manipulation language (DML) event. DML events are INSERT, UPDATE, or DELETE statements on a table or view. These triggers fire when any valid event is fired, regardless of whether or not any table rows are affected.

Two type of Trigger

INSTEAD OF and AFTER,

An INSTEAD OF trigger is the one that is usually associated with a view, and runs on an UPDATE action placed on that view.

An AFTER trigger fires after a modification action has occurred.

Syntax of a Trigger CREATE TRIGGER name ON table   [WITH ENCRYPTION]   [FOR/AFTER/INSTEAD OF]   [INSERT, UPDATE, DELETE]   [NOT FOR REPLICATION]ASBEGIN--SQL statements...END

 

Sample Example   CREATE TRIGGER [TRIGGER_ALTER_COUNT] ON [dbo].[tblTriggerExample] FOR INSERT, UPDATEASBEGINEND

haytham Hamdy
by haytham Hamdy , Developer , SAP

A special kind of stored procedure which executed automatically after/instead of a specific event occur in the database

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.