Monday, August 31, 2009

MCTS 70-433 SQL Server DML Triggers - INSTEAD OF UPDATE for validation?

This DML trigger allows you add complex validation code around your update statement:


create trigger checksalary on employee

instead of update as if update(salary)

update employee set salary ...



The following article gives a good overview of trigger in SQL Server:


Triggers are of 3 types in SQL Server 2005:

1.    DML Triggers

.         AFTER Triggers

.         INSTEAD OF Triggers

2.    DDL Triggers

3.    CLR Triggers


Note:DDL and CLR Triggers cannot work in SQL Server 2000

DML Trigger:-These Trigger is fired only when INSERT, UPDATE, and DELETE Statement occurs in table.


Explanation on DML Trigger:

Let us create a Table and insert some records in that Table.

1) After Triggers:

After Triggers can be created in 3 ways.

1)    After INSERT

2)    After UPDATE

3)    After DELETE


1) creating After INSERT Trigger:-


Syntax:

create trigger triggername

on tablename

AFTER INSERT

As

[SQL Statement/PRINT command]

GO


Eg: 

create trigger afterinsert_trigger

on emp

AFTER INSERT

as

PRINT 'AFTER TRIGGER EXECUTED SUCESSFULLY'

GO


http://www.dotnetspark.com/kb/657-how-to-create-trigger.aspx


No comments: