Sunday, December 16, 2012

How to create trigger in sql server 2005,2008:

A trigger is  a database object which is associated with table.A trigger is automatically executed whenever a insert, update and delete operation is performed in table.Triggers are also know as 'special  store procedure' but   they are  associated with table's    only .

Syntax of trigger creation in sql server 2005,2008 is given below:

CREATE TRIGGER <Schema_Name, sysname, Schema_Name>.<Trigger_Name, sysname, Trigger_Name>
   ON  <Schema_Name, sysname, Schema_Name>.<Table_Name, sysname, Table_Name>
   AFTER <Data_Modification_Statements, , INSERT,DELETE,UPDATE>
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for trigger here
END

Exapmle: 
 Approach 1:Trigger for  insert

Create  a new table in which you have to apply a trigger:
CREATE TABLE Class_Master (Student_ID int IDENTITY, SName varchar(100),Age int,Class int)

Create trigger for this table:

CREATE TRIGGER tr_Class_Master_INSERT
ON Class_Master
FOR INSERT
AS
declare @class varchar(50)
select @class=Class from inserted

IF (@class>=9 and @class<=10
 BEGIN
   insert into TblHighSchool
    select * from inserted
 END
Else IF(@class>10 and @class<=12)
    Insert into TblSeniorHighSecondry
    select * from inserted
else
    Insert into TblJunior
    select * from inserted

In the above table we are inserting the student information in a table  Class_Master and on new data insert we are checking class in a trigger and then as per student class we are placing them in other tables.

Approach 2:Trigger for Delete

In below example we are deleting the records from child tables when the data is deleted from Class_master table

CREATE TRIGGER tr_Class_Master_Delete
ON Class_Master
FOR Delete
AS
declare @class varchar(50),@Student_ID int
select @class=Class,@Student_ID=Student_ID from deleted

 IF (@class>=9 and @class<=10) 
 BEGIN
    delete from TblHighSchool where Student_ID=@Student_ID
   
 END
 Else IF(@class>10 and @class<=12)
    delete from TblSeniorHighSecondry where Student_ID=@Student_ID  

 else
    delete from TblJunior where Student_ID=@Student_ID 



Approach 3:Trigger for Update:

how to   use update trigger in sql server 2005,2008:
Sql server update trigger: 

In below example we are updating the age of child table if age is updated in master table 'Class_master'.

CREATE TRIGGER tr_Class_Master_Update
ON Class_Master
AFTER Update
AS
declare @age int ,@Student_ID int,@class varchar(50)
select @age=age,@Student_ID=Student_ID,@class=class from inserted

 IF (@class>=9 and @class<=10) 
 BEGIN
     update  TblHighSchool set age=@age where Student_ID=@Student_ID
   
 END
 Else IF(@class>10 and @class<=12)
    update TblSeniorHighSecondry set age=@age where Student_ID=@Student_ID   

 else
    update   TblJunior set age=@age where Student_ID=@Student_ID



So above examples shows that how to use trigger for insert ,update and delete query.These automatically fire whenever a insert ,update or delete statement is performed in table.
   





No comments:

Post a Comment