Go to DotNetFunda.com
 Online : 1339 |  Welcome, Guest!   Login
 
Home > Articles > Sql Server > TRIGGERS

Submit Article | Articles Home | Search Articles |

TRIGGERS

1 vote(s)
Rating: 3 out of 5
red flag  Posted on: 6/29/2009 3:02:47 AM by Syedshakeer | Views: 1324 | Category: Sql Server | Level: Intermediate


In this Article you can learn how to create and use DML TRIGGERS .



Types of Triggers

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

When you execute the afterinsert_trigger it gives message as ‘The Command(s) created successfully
Now insert one record in a emp table. You can see the trigger will be fired automatically when the row is inserted in a table successfully.

Creating AFTER UPDATE TRIGGER:-

 

create trigger afterupdate_trigger

on emp

AFTER UPDATE

as

PRINT 'AFTER UPDATE TRIGGER EXECUTED SUCESSFULLY'

GO




Creating AFTER DELETE TRIGGER:

 

Create trigger afterdelete_trigger

On emp

AFTER DELETE

as

PRINT 'AFTER DELETE TRIGGER EXECUTED SUCESSFULLY'

GO



INSTEAD OF TRIGGER:-

 

Syntax:

create trigger triggername

on tablename

INSTEAD OF INSERT

As

[SQL Statement/PRINT command]

 

GO

 

Example:-

create trigger insteadofinsert_trigger

on emp

INSTEAD OF INSERT

as

PRINT 'INSTEAD OF INSERT TRIGGER EXECUTED SUCESSFULLY'

GO




Creating INSTEAD OF UPDATE TRIGGER:-

 

create trigger insteadofupdate_trigger

on emp

INSTEAD OF UPDATE

as

PRINT 'INSTEAD OF UPDATE TRIGGER EXECUTED SUCESSFULLY'

GO





Creating INSTEAD OF DELETE TRIGGER:-

 

create trigger insteadofdelete_trigger

on emp

INSTEAD OF DELETE

as

PRINT 'INSTEAD OF DELETE TRIGGER EXECUTED SUCESSFULLY'

GO



HOW TO Drop a Trigger?

 

Syntax: DROP TRIGGER triggername

 

Eg: DROP TRIGGER insteadofdelete_trigger




If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Found interesting? Add this to:

| More



Please Sign In to vote for this post.

 
Latest post(s) from Syedshakeer

Latest Articles
Experience:2 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, February 05, 2009
Level:Starter
Status: [Member]
Biography:Shakeer Hussain has completed his Master of Computer Applications degree from Deccan College of engg and technology of Osmania University.He is a MVM of www.dotnetspider.com.He has good experience in the areas of ASP.NET, C#.NET, VB.NET, SQL SERVER 2000/2005 and Windows Mobile. He has worked in Windows Mobile,Web Applicatin and ERP projects.

Submit Article

About Us | The Team | Advertise | Contact Us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 9/3/2010 4:04:20 AM