SQL Server Performance

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


Article Topics

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

Training Videos

Check out our new SQL Server Training Videos section More...

Write for Us

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

50 Tips to Boost Performance of an ASP.NET Application - ...
50 Tips to Boost Performance of an ASP.NET Application - ...
Understanding WCF Hosting
SQL Server Logical Reads – What do they really tell us? ...

More     
 
Latest FAQ's

Will Check Constraints Improve Database Performance?
The Excel Connection Manager is not supported in the 64-bit version ...
Is there a difference between fill factor 0 and 100 ...
An ASP.NET setting has been detected that does not apply in ...

More     
   
Latest Software Reviews

Spotlight on ApexSQL Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Data Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Doc 2008
ApexSQL Enforce

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>>    








C# Help and Tutorials | PHP MySQL Tutorial | Sharepoint Tutorial | Azure Tutorial | Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | SQL Server Training Videos | 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


              © 2010 Jude O'Kelly. All rights reserved