Sunteți pe pagina 1din 5

10/5/13

10,125,076 members (44,205 online)

Triggers -- Sql Server - CodeProject


Sign in

home

articles

quick answers

discussions

features

community

help

Search for articles, questions, tips

Articles Database Database SQL Server

Next

Article Browse Code Stats Revisions Alternatives Comments & Discussions (87)

Triggers -- Sql Server


By s.india , 26 Apr 2008
4.89 (90 votes)
Like 36 11 Tw eet 1

About Article
Sign Up to vote This article gives a brief introduction about Triggers in Sql Server 2000/2005. Type Licence First Posted Views Bookmarked Article CPOL 26 Apr 2008 372,228 106 times

INTRODUCTION
TRIGGERS IN SQL SERVER

BACKGROUND
This article gives a brief introduction about Triggers in Sql Server 2000/2005.

SQL2000 SQL2005 SQL-Server , +

What is a Trigger
A trigger is a special kind of a store procedure that executes in response to certain action on the table like insertion, deletion or updation of data. It is a database object which is bound to a table and is executed automatically. You cant explicitly invoke triggers. The only way to do this is by performing the required action no the table that they are assigned to.

Types Of Triggers
There are three action query types that you use in SQL which are INSERT, UPDATE and DELETE. So, there are three types of triggers and hybrids that come from mixing and matching the events and timings that fire them. Basically, triggers are classified into two main types:(i) After Triggers (For Triggers) (ii) Instead Of Triggers

Top News
Finally: Bill Gates admits Control-Alt-Delete was a mistake
Get the Insider News free each morning.

Related Videos

(i) After Triggers


These triggers run after an insert, update or delete on a table. They are not supported for views. AFTER TRIGGERS can be classified further into three types as: (a) AFTER INSERT Trigger. (b) AFTER UPDATE Trigger. (c) AFTER DELETE Trigger. Lets create After triggers. First of all, lets create a table and insert some sample data. Then, on this table, I will be attaching several triggers.
Collapse | Copy Code

C R E A T ET A B L EE m p l o y e e _ T e s t ( E m p _ I DI N TI d e n t i t y , E m p _ n a m eV a r c h a r ( 1 0 0 ) , E m p _ S a lD e c i m a l( 1 0 , 2 ) ) I N S E R TI N T OE m p l o y e e _ T e s tV A L U E S( ' A n e e s ' , 1 0 0 0 ) ; I N S E R TI N T OE m p l o y e e _ T e s tV A L U E S( ' R i c k ' , 1 2 0 0 ) ; I N S E R TI N T OE m p l o y e e _ T e s tV A L U E S( ' J o h n ' , 1 1 0 0 ) ; I N S E R TI N T OE m p l o y e e _ T e s tV A L U E S( ' S t e p h e n ' , 1 3 0 0 ) ; I N S E R TI N T OE m p l o y e e _ T e s tV A L U E S( ' M a r i a ' , 1 4 0 0 ) ;

Related Articles
Matrix Multiplication in C# Creating animations with Dundas Chart for ASP.NET Smarter Data Labels with Dundas Chart SmartLabels Understanding Chart Areas with Dundas Chart for .NET A Formatted Text Box Using screensavers inside the Windows Media Player Making Sense of Geographic Data with Dundas Map and AJAX

I will be creating an AFTER INSERT TRIGGER which will insert the rows inserted into the table into another audit table. The main purpose of this audit table is to record the changes in the main table. This can be thought of as a generic audit trigger.

www.codeproject.com/Articles/25600/Triggers-Sql-Server

1/5

10/5/13
Now, create the audit table as:C R E A T ET A B L EE m p l o y e e _ T e s t _ A u d i t ( E m p _ I Di n t , E m p _ n a m ev a r c h a r ( 1 0 0 ) , E m p _ S a ld e c i m a l( 1 0 , 2 ) , A u d i t _ A c t i o nv a r c h a r ( 1 0 0 ) , A u d i t _ T i m e s t a m pd a t e t i m e )

Triggers -- Sql Server - CodeProject


Collapse | Copy Code

Handling connection notification between a desktop machine and Windows CE based devices Create data-driven applications with the Hera Application Framework Towards the self-documenting database: extended properties Accessibility audit vs. accessibility testing Digital Signatures and PDF Documents

(a) AFTRE INSERT Trigger


This trigger is fired after an INSERT on the table. Lets create the trigger as:C R E A T ET R I G G E Rt r g A f t e r I n s e r tO N[ d b o ] . [ E m p l o y e e _ T e s t ] F O RI N S E R T A S d e c l a r e@ e m p i di n t ; d e c l a r e@ e m p n a m ev a r c h a r ( 1 0 0 ) ; d e c l a r e@ e m p s a ld e c i m a l ( 1 0 , 2 ) ; d e c l a r e@ a u d i t _ a c t i o nv a r c h a r ( 1 0 0 ) ; s e l e c t@ e m p i d = i . E m p _ I Df r o mi n s e r t e di ; s e l e c t@ e m p n a m e = i . E m p _ N a m ef r o mi n s e r t e di ; s e l e c t@ e m p s a l = i . E m p _ S a lf r o mi n s e r t e di ; s e t@ a u d i t _ a c t i o n = ' I n s e r t e dR e c o r d-A f t e rI n s e r tT r i g g e r . ' ; i n s e r ti n t oE m p l o y e e _ T e s t _ A u d i t ( E m p _ I D , E m p _ N a m e , E m p _ S a l , A u d i t _ A c t i o n , A u d i t _ T i m e s t a m p ) v a l u e s ( @ e m p i d , @ e m p n a m e , @ e m p s a l , @ a u d i t _ a c t i o n , g e t d a t e ( ) ) ; G O P R I N T' A F T E RI N S E R Tt r i g g e rf i r e d . '
Collapse | Copy Code

Color Scale Filter WMP Power Hour APP Merge Landscape and Portrait PDFs using ASP.NET How to conduct an SMS survey using a cell phone connected SMS gateway and MS Access Using Barcodes in Documents Best Practices How to Retrieve EMC Centera Cluster/Pool Capabilities Embedding IronPython in WPF Using C# "Hey! Is That My Car? How to Sharpen a QuickBird Satellite Image Using DotImage" Integrate your SharePoint environment into the open standards-based WebSphere Portal platform using the Visual Studio IDE

The CREATE TRIGGER statement is used to create the trigger. THE ON clause specifies the table name on which the trigger is to be attached. The FOR INSERT specifies that this is an AFTER INSERT trigger. In place of FOR INSERT, AFTER INSERT can be used. Both of them mean the same. In the trigger body, table named inserted has been used. This table is a logical table and contains the row that has been inserted. I have selected the fields from the logical inserted table from the row that has been inserted into different variables, and finally inserted those values into the Audit table. To see the newly created trigger in action, lets insert a row into the main table as :
i n s e r ti n t oE m p l o y e e _ T e s tv a l u e s ( ' C h r i s ' , 1 5 0 0 ) ;

Collapse | Copy Code

Now, a record has been inserted into the Employee_Test table. The AFTER INSERT trigger attached to this table has inserted the record into the Employee_Test_Audit as:6 C h r i s 1 5 0 0 . 0 0 I n s e r t e dR e c o r d-A f t e rI n s e r tT r i g g e r . 2 0 0 8 0 4 2 61 2 : 0 0 : 5 5 . 7 0 0

Collapse | Copy Code

(b) AFTER UPDATE Trigger


This trigger is fired after an update on the table. Lets create the trigger as:C R E A T ET R I G G E Rt r g A f t e r U p d a t eO N[ d b o ] . [ E m p l o y e e _ T e s t ] F O RU P D A T E A S d e c l a r e@ e m p i di n t ; d e c l a r e@ e m p n a m ev a r c h a r ( 1 0 0 ) ; d e c l a r e@ e m p s a ld e c i m a l ( 1 0 , 2 ) ; d e c l a r e@ a u d i t _ a c t i o nv a r c h a r ( 1 0 0 ) ; s e l e c t@ e m p i d = i . E m p _ I Df r o mi n s e r t e di ; s e l e c t@ e m p n a m e = i . E m p _ N a m ef r o mi n s e r t e di ; s e l e c t@ e m p s a l = i . E m p _ S a lf r o mi n s e r t e di ; i fu p d a t e ( E m p _ N a m e ) s e t@ a u d i t _ a c t i o n = ' U p d a t e dR e c o r d-A f t e rU p d a t eT r i g g e r . ' ; i fu p d a t e ( E m p _ S a l ) s e t@ a u d i t _ a c t i o n = ' U p d a t e dR e c o r d-A f t e rU p d a t eT r i g g e r . ' ; i n s e r ti n t oE m p l o y e e _ T e s t _ A u d i t ( E m p _ I D , E m p _ N a m e , E m p _ S a l , A u d i t _ A c t i o n , A u d i t _ T i m e s t a m p ) v a l u e s ( @ e m p i d , @ e m p n a m e , @ e m p s a l , @ a u d i t _ a c t i o n , g e t d a t e ( ) ) ; G O P R I N T' A F T E RU P D A T ET r i g g e rf i r e d . '
Collapse | Copy Code

The AFTER UPDATE Trigger is created in which the updated record is inserted into the audit table. There is no logical table updated like the logical table inserted. We can obtain the updated value of a field from the update(column_name) function. In our trigger, we have used, if update(Emp_Name) to check if the column Emp_Name has been updated. We have similarly checked the column Emp_Sal for an update. Lets update a record column and see what happens.
u p d a t eE m p l o y e e _ T e s ts e tE m p _ S a l = 1 5 5 0w h e r eE m p _ I D = 6

Collapse | Copy Code

www.codeproject.com/Articles/25600/Triggers-Sql-Server

2/5

10/5/13
This inserts the row into the audit table as:-

Triggers -- Sql Server - CodeProject


Collapse | Copy Code

6 C h r i s 1 5 5 0 . 0 0 U p d a t e dR e c o r d-A f t e rU p d a t eT r i g g e r .

2 0 0 8 0 4 2 61 2 : 3 8 : 1 1 . 8 4 3

(c) AFTER DELETE Trigger


This trigger is fired after a delete on the table. Lets create the trigger as:C R E A T ET R I G G E Rt r g A f t e r D e l e t eO N[ d b o ] . [ E m p l o y e e _ T e s t ] A F T E RD E L E T E A S d e c l a r e@ e m p i di n t ; d e c l a r e@ e m p n a m ev a r c h a r ( 1 0 0 ) ; d e c l a r e@ e m p s a ld e c i m a l ( 1 0 , 2 ) ; d e c l a r e@ a u d i t _ a c t i o nv a r c h a r ( 1 0 0 ) ; s e l e c t@ e m p i d = d . E m p _ I Df r o md e l e t e dd ; s e l e c t@ e m p n a m e = d . E m p _ N a m ef r o md e l e t e dd ; s e l e c t@ e m p s a l = d . E m p _ S a lf r o md e l e t e dd ; s e t@ a u d i t _ a c t i o n = ' D e l e t e d-A f t e rD e l e t eT r i g g e r . ' ; i n s e r ti n t oE m p l o y e e _ T e s t _ A u d i t ( E m p _ I D , E m p _ N a m e , E m p _ S a l , A u d i t _ A c t i o n , A u d i t _ T i m e s t a m p ) v a l u e s ( @ e m p i d , @ e m p n a m e , @ e m p s a l , @ a u d i t _ a c t i o n , g e t d a t e ( ) ) ; G O P R I N T' A F T E RD E L E T ET R I G G E Rf i r e d . '
Collapse | Copy Code

In this trigger, the deleted records data is picked from the logical deleted table and inserted into the audit table. Lets fire a delete on the main table. A record has been inserted into the audit table as:6 C h r i s 1 5 5 0 . 0 0 D e l e t e d-A f t e rD e l e t eT r i g g e r . 2 0 0 8 0 4 2 61 2 : 5 2 : 1 3 . 8 6 7

Collapse | Copy Code

All the triggers can be enabled/disabled on the table using the statement
A L T E RT A B L EE m p l o y e e _ T e s t{ E N A B L E | D I S B A L E }T R I G G E RA L L

Collapse | Copy Code

Specific Triggers can be enabled or disabled as :A L T E RT A B L EE m p l o y e e _ T e s tD I S A B L ET R I G G E Rt r g A f t e r D e l e t e

Collapse | Copy Code

This disables the After Delete Trigger named trgAfterDelete on the specified table.

(ii) Instead Of Triggers


These can be used as an interceptor for anything that anyonr tried to do on our table or view. If you define an Instead Of trigger on a table for the Delete operation, they try to delete rows, and they will not actually get deleted (unless you issue another delete instruction from within the trigger) INSTEAD OF TRIGGERS can be classified further into three types as:(a) INSTEAD OF INSERT Trigger. (b) INSTEAD OF UPDATE Trigger. (c) INSTEAD OF DELETE Trigger. (a) Lets create an Instead Of Delete Trigger as:C R E A T ET R I G G E Rt r g I n s t e a d O f D e l e t eO N[ d b o ] . [ E m p l o y e e _ T e s t ] I N S T E A DO FD E L E T E A S d e c l a r e@ e m p _ i di n t ; d e c l a r e@ e m p _ n a m ev a r c h a r ( 1 0 0 ) ; d e c l a r e@ e m p _ s a li n t ; s e l e c t@ e m p _ i d = d . E m p _ I Df r o md e l e t e dd ; s e l e c t@ e m p _ n a m e = d . E m p _ N a m ef r o md e l e t e dd ; s e l e c t@ e m p _ s a l = d . E m p _ S a lf r o md e l e t e dd ; i f ( @ e m p _ s a l > 1 2 0 0 ) b e g i n R A I S E R R O R ( ' C a n n o td e l e t ew h e r es a l a r y>1 2 0 0 ' , 1 6 , 1 ) ; R O L L B A C K ; e n d e l s e b e g i n d e l e t ef r o mE m p l o y e e _ T e s tw h e r eE m p _ I D = @ e m p _ i d ; C O M M I T ; i n s e r ti n t o E m p l o y e e _ T e s t _ A u d i t ( E m p _ I D , E m p _ N a m e , E m p _ S a l , A u d i t _ A c t i o n , A u d i t _ T i m e s t a m p ) v a l u e s ( @ e m p _ i d , @ e m p _ n a m e , @ e m p _ s a l , ' D e l e t e d-I n s t e a dO fD e l e t e T r i g g e r . ' , g e t d a t e ( ) ) ; P R I N T' R e c o r dD e l e t e d-I n s t e a dO fD e l e t eT r i g g e r . ' e n d B E G I N

Collapse | Copy Code

www.codeproject.com/Articles/25600/Triggers-Sql-Server

3/5

10/5/13
G O E N D

Triggers -- Sql Server - CodeProject

This trigger will prevent the deletion of records from the table where Emp_Sal > 1200. If such a record is deleted, the Instead Of Trigger will rollback the transaction, otherwise the transaction will be committed. Now, lets try to delete a record with the Emp_Sal >1200 as:Collapse | Copy Code

d e l e t ef r o mE m p l o y e e _ T e s tw h e r eE m p _ I D = 4

This will print an error message as defined in the RAISE ERROR statement as:Collapse | Copy Code

S e r v e r :M s g5 0 0 0 0 ,L e v e l1 6 ,S t a t e1 ,P r o c e d u r et r g I n s t e a d O f D e l e t e ,L i n e1 5 C a n n o td e l e t ew h e r es a l a r y>1 2 0 0

And this record will not be deleted. In a similar way, you can code Instead of Insert and Instead Of Update triggers on your tables.

CONCLUSION
In this article, I took a brief introduction of triggers, explained the various kinds of triggers After Triggers and Instead Of Triggers along with their variants and explained how each of them works. I hope you will get a clear understanding about the Triggers in Sql Server and their usage.

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

s.india
Software Developer (Senior) India No Biography provided Article Top

Comments and Discussions


You must Sign In to use this message board. Search this forum Profile popups Spacing Relaxed Noise Very High Layout Normal Per page 10 Go Update

First Prev Next

My vote of 5 very good My vote of 5

Akhilesh7799 Member 10289960 AVINCODE

24-Sep-13 21:39 22-Sep-13 3:46 18-Sep-13 23:27

www.codeproject.com/Articles/25600/Triggers-Sql-Server

4/5

10/5/13
My vote of 5 My vote of 5 triggers for select statement code is not working My vote of 5 My vote of 5 My vote of 5
Last Visit: 31-Dec-99 18:00 General News

Triggers -- Sql Server - CodeProject


Pratik Bhuva Deepak Rana11 srinivaskumar pasumarthi kushal Vashist Shivarajbk Member 9972358 Chakri Reddy Last Update: 4-Oct-13 14:41 Suggestion Question Bug Answer Refresh Joke Rant 27-Aug-13 2:21 29-Jul-13 21:53 3-Jul-13 0:55

25-Jun-13 1:43 20-Jun-13 0:54 3-Jun-13 1:02 21-May-13 19:23 1 2 3 4 5 6 7 8 9 Next Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Permalink | Advertise | Privacy | Mobile Web03 | 2.6.1309030.1 | Last Updated 26 Apr 2008 Layout: fixed | fluid Article Copyright 2008 by s.india Everything else Copyright CodeProject, 1999-2013 Terms of Use

www.codeproject.com/Articles/25600/Triggers-Sql-Server

5/5

S-ar putea să vă placă și