Site sponsored by: Idera The gold standard of SQL Server performance monitoring & diagnostics.
SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Quiz
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
Developer
General DBA
ASP.NET / ADO.NET

Write for Us

Share your SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

System Data Collection Reports
Recover Data Using Database Snapshots
Analyze and Fix Index Fragmentation in SQL Server 2008
Powerful Geographical Visualisations made easy with SQL 2008 Spatial (Part 2) ...

More     
 
Latest FAQ's

How to alter a User Defined Data Type?
How to unzip a File in SSIS?
How to view previous query plans?
ALTER TABLE SWITCH statement failed because the object '%.*ls' is not ...

More     
   
Latest Software Reviews

Spotlight on ApexSQL Doc 2008
ApexSQL Enforce
Embarcadero Change Manager
SQL Server DBA Dashboard

More     

articles >> developer >> Implementing Triggers in SQL Server 2000 ...

Implementing Triggers in SQL Server 2000

By : Navneeth Diwaker Naik
Aug 31, 2004

Page 2 / 6

AFTER Triggers

The type of trigger that gets executed automatically after the statement that triggered it completes is called an AFTER trigger. An AFTER trigger is a trigger that gets executed automatically before the transaction is committed or rolled back.

Using the below script, first we shall create a trigger on the table USER_MASTER for the INSERT event of the table.

 

USE KDMNN

Go

 

CREATE TRIGGER trgInsert

ON User_Master

FOR INSERT

AS

      Print ('AFTER Trigger [trgInsert]  – Trigger executed !!')

GO

 

BEGIN TRANSACTION

DECLARE @ERR INT

 

INSERT INTO USER_MASTER(USERNAME, PASSWORD)

VALUES('Damerla','Damerla')

 

SET @ERR = @@Error

IF @ERR = 0   

             BEGIN

                           ROLLBACK TRANSACTION

                           PRINT 'ROLLBACK TRANSACTION'

             END

ELSE

             BEGIN

                           COMMIT TRANSACTION

                           PRINT 'COMMIT TRANSACTION'

             END

Output

AFTER Trigger [trgInsert]  – Trigger executed !!

(1 row(s) affected)

ROLLBACK TRANSACTION

By looking at the output, we can conclude that before the transaction is rolled back or committed, the AFTER trigger gets executed automatically. A table can have several AFTER triggers for each of the three triggering actions i.e., INSERT, DELETE and UPDATE. Using the below script, we shall create two triggers on the table User_Master for the INSERT triggering action.

 

CREATE TRIGGER trgInsert2

ON User_Master

FOR INSERT

AS

BEGIN

     Print ('AFTER Trigger [trgInsert2]  – Trigger executed !!')

END

GO

 

CREATE TRIGGER trgInsert3

ON User_Master

FOR INSERT

AS

BEGIN

     Print ('AFTER Trigger [trgInsert3]  – Trigger executed !!')

END

GO

 

BEGIN TRANSACTION

DECLARE @ERR INT

 

INSERT INTO USER_MASTER(USERNAME, PASSWORD)

VALUES('Damerla','Damerla')

 

SET @ERR = @@Error

IF @ERR = 0   

             BEGIN

                           ROLLBACK TRANSACTION

                           PRINT 'ROLLBACK TRANSACTION'

             END

ELSE

             BEGIN

                           COMMIT TRANSACTION

                           PRINT 'COMMIT TRANSACTION'

             END

Output

AFTER Trigger [trgInsert]  – Trigger executed !!

AFTER Trigger [trgInsert2]  – Trigger executed !!

AFTER Trigger [trgInsert3]  – Trigger executed !!

(1 row(s) affected)

ROLLBACK TRANSACTION


<< Prev Page     Next Page>>    








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 1999-2008 by T10 Media. All rights reserved