Implementing Triggers in SQL Server 2000

Triggers are special types of Stored Procedures that are defined to execute automatically in place of or after data modifications. They can be executed automatically on the INSERT, DELETE and UPDATE triggering actions.

There are two different types of triggers in Microsoft SQL Server 2000. They are INSTEAD OF triggers and AFTER triggers. These triggers differ from each other in terms of their purpose and when they are fired. In this article we shall discuss each type of trigger.

First of all, let’s create a sample database with some tables and insert some sample data in those tables using the script below:

Create Database KDMNN

GO

USE KDMNN

GO

CREATE TABLE [dbo].[User_Details] (

             [UserID] [int] NULL ,

             [FName] [varchar] (50) NOT NULL ,

             [MName] [varchar] (50) NULL ,

             [LName] [varchar] (50) NOT NULL ,

             [Email] [varchar] (50)  NOT NULL

) ON [PRIMARY]

GO

CREATE TABLE [dbo].[User_Master] (

             [UserID] [int] IDENTITY (1, 1) NOT NULL ,

             [UserName] [varchar] (50) NULL ,

             [Password] [varchar] (50) NULL

) ON [PRIMARY]

GO

ALTER TABLE [dbo].[User_Master] WITH NOCHECK ADD

             CONSTRAINT [PK_User_Master] PRIMARY KEY  CLUSTERED

             (

                           [UserID]

             )  ON [PRIMARY]

GO

ALTER TABLE [dbo].[User_Details] ADD

             CONSTRAINT [FK_User_Details_User_Master] FOREIGN KEY

             (

                           [UserID]

             ) REFERENCES [dbo].[User_Master] (

                           [UserID]

             )

GO

INSERT INTO USER_MASTER(USERNAME, PASSWORD)

             SELECT ‘Navneeth’,’Navneeth’ UNION

             SELECT ‘Amol’,’Amol’ UNION

             SELECT ‘Anil’,’Anil’ UNION

             SELECT ‘Murthy’,’Murthy’

INSERT INTO USER_DETAILS(USERID, FNAME, LNAME, EMAIL)

             SELECT 1,’Navneeth’,’Naik’,’navneeth@kdmnn.com’ UNION

             SELECT 2,’Amol’,’Kulkarni’,’amol@kdmnn.com’ UNION

             SELECT 3,’Anil’,’Bahirat’,’anil@kdmnn.com’ UNION

             SELECT 4,’Murthy’,’Belluri’,’murthy@kdmnn.com’

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 |