.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> SQL Query >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Samples of Triggers in SQL Server

Posted By:Suresh       Posted Date: May 24, 2010    Points: 10    Category: SQL Query    URL: http://www.dotnetspark.com  

Basic of triggers in SQL Server, We will see how to create trigger in a simple way. It will be very useful for beginners
 

Hello friends,
  This is sample trigger.Once you gothrough this code, you people can easily come to know about the trigger. If any one have doubt regarding this.Let me Know.I vil guide you.
  
CREATE TABLE Employee_Test
(
Emp_ID INT Identity,
Emp_name Varchar(100),
Emp_Sal Decimal (10,2)
)

INSERT INTO Employee_Test VALUES ('Anees',1000);
INSERT INTO Employee_Test VALUES ('Rick',1200);
INSERT INTO Employee_Test VALUES ('John',1100);
INSERT INTO Employee_Test VALUES ('Stephen',1300);
INSERT INTO Employee_Test VALUES ('Maria',1400);




CREATE TABLE Employee_Test_Audit
(
Emp_ID int,
Emp_name varchar(100),
Emp_Sal decimal (10,2),
Audit_Action varchar(100),
Audit_Timestamp datetime
)




Create TRIGGER trgAfterInsert ON [Employee_Test]
FOR INSERT,DELETE,UPDATE
AS
    declare @empid int;
    declare @empname varchar(100);
    declare @empsal decimal(10,2);
    declare @audit_action varchar(100);

IF (select count(*) from inserted) <> 0 and (select count(*) from deleted) = 0  --Insert
Begin
    
    Select @empid=i.Emp_ID,@empname=i.Emp_Name,@empsal=i.Emp_Sal  from inserted i;    
    Set @audit_action='Inserted Record -- After Insert Trigger.';
    insert into Employee_Test_Audit
           (Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
    values(@empid,@empname,@empsal,@audit_action,getdate());
    PRINT 'AFTER INSERT trigger fired.'
End
IF (Select count(*) from inserted) <> 0 and (select count(*) from deleted) <> 0 --update
Begin
    Select @empid=i.Emp_ID,@empname=i.Emp_Name,@empsal=i.Emp_Sal  from inserted i;    
    Set @audit_action='Updated Record -- After Update Trigger.';
    Update Employee_Test_Audit Set Emp_Name=@empname,Emp_Sal=@empsal,
    Audit_Action=@audit_action,Audit_Timestamp=getdate() where Emp_ID=@empid
    PRINT 'AFTER Update trigger fired.'
End
IF (select count(*) from inserted) = 0 and (select count(*) from deleted) <> 0 --delete
Begin
    Select @empid=i.Emp_ID,@empname=i.Emp_Name,@empsal=i.Emp_Sal  from Deleted i;    
    Set @audit_action='Deleted Record -- After Deleted Trigger.';
            insert into Employee_Test_Audit
            (Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
            values(@empid,@empname,@empsal,@audit_action,getdate());
    PRINT 'AFTER Delete trigger fired.'
End


---------------------------------------------------



select * from Employee_Test
Select * from Employee_Test_Audit

--------------------
Go
update Employee_Test set emp_name='Mani' where emp_id=1
Go
Select * from Employee_Test_Audit

Go
Delete from Employee_Test where emp_id=1

select * from Employee_Test
Select * from Employee_Test_Audit


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply

    Read also another Resources from the same Author

Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend