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

  • Nominate yourself for "Agile Software Development using Scrum" online session for FREE, click here.

  • Download OOPS and ASP.NET Online training session video and PPT from here.

Submit Article | Articles Home | Search Articles |

TRIGGERS

 Posted on: 6/29/2009 3:02:47 AM by Syedshakeer | Views: 863 | Category: Sql Server | Level: Intermediate | Print Article
In this Article you can learn how to create and use DML TRIGGERS .

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

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.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:1 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, February 05, 2009
Level:Bronze
Status: [Member]
Biography:Hi to All ..
I am Working as Software Developer on .NET and MVM of www.dotnetspider.com
 Latest post(s) from Syedshakeer

   ◘ How to get the value of a Hidding column in a Gridview using C# posted on 9/8/2009 6:05:36 PM
   ◘ How to calculate total at the BackEnd using Trigger? posted on 8/25/2009 6:14:14 PM
   ◘ How to display records as First-Next-Previous-Last in a Textboxes using Windows Application? posted on 8/25/2009 6:01:05 PM
   ◘ How to Start Mobile Application on Windows? posted on 8/18/2009 1:00:01 AM
   ◘ Paging for First, Next, Previous and Last in gridview posted on 8/16/2009 12:47:43 PM


Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)