Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 29985 |  Welcome, Guest!   Register  Login
Home > Articles > Sql Server > TRIGGERS

TRIGGERS

1 vote(s)
Rating: 3 out of 5
Article posted by Syedshakeer on 6/29/2009 | Views: 2704 | Category: Sql Server | Level: Intermediate red flag


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.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

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.
 Responses
Posted by: Raja | Posted on: 13 Feb 2011 10:57:25 PM | Points: 25

Short and sweet article, for in depth knowledge on triggers readers can visit http://msdn.microsoft.com/en-us/library/ms189799.aspx.

Thanks!

>> Write Response - Respond to this post and get points
Related Posts

This article describes how to update Remote SQL Server Databases with a Single Click.

Most of us worry about Database Backup is Time taken, free space required for the backup file etc. etc.

This is part 33 of the series of article on SSIS. In this article we are going to see on how to use an Aggregate (SUM) data flow transformation control in SSIS packaging.

This post describes how to use XML DataType and shows varrious methods to query it.

In this article, I shall show how to concatenate data from COLUMN1 by grouping against COLUMN2 without looping CURSOR.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for 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. | 5/28/2012 11:57:53 AM