Implementing Triggers in SQL Server 2000

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

Continues…

Pages: 1 2 3 4 5 6




Related Articles :

  • No Related Articles Found

No comments yet... Be the first to leave a reply!

Software Reviews | Book Reviews | FAQs | Tips | Articles | Performance Tuning | Audit | BI | Clustering | Developer | Reporting | DBA | ASP.NET Ado | Views tips | | Developer FAQs | Replication Tips | OS Tips | Misc Tips | Index Tuning Tips | Hints Tips | High Availability Tips | Hardware Tips | ETL Tips | Components Tips | Configuration Tips | App Dev Tips | OLAP Tips | Admin Tips | Software Reviews | Error | Clustering FAQs | Performance Tuning FAQs | DBA FAQs |