Click here to Skip to main content
15,880,405 members
Articles / Web Development / ASP.NET
Tip/Trick

Sqlserver Trigger a quick look

Rate me:
Please Sign up or sign in to vote.
2.37/5 (11 votes)
23 Apr 2011CPOL 15.1K   2   5
Sqlserver Trigger
A database trigger is procedural code that is automatically executed in response to certain events on a particular table or view in a database.

Triggers that run after an update, insert, or delete. .Triggers are used to enforce data integrity and business rules such as automatically updating summary data. It allows to perform cascading delete or update operations.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[trgAfterInsert] ON [dbo].[UserTable] 
FOR INSERT
AS
	declare @empid int;
	declare @empname varchar(100);
	
	declare @audit_action varchar(100);

	select @empid=i.ID from inserted i;	
	select @empname=i.UserName from inserted i;	
	
	set @audit_action='Inserted Record -- After Insert Trigger.';


INSERT INTO [TestPB].[dbo].[tt]
           ([id]
           ,[name]
           ,[audit_action])
     VALUES
           (@empid,
           @empname
           ,@audit_action)


	PRINT 'AFTER INSERT trigger fired.'


Hope this can help You friends.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer India
India India
http://devcorners.com/
Total DotNet/Programming Solution

I am Prasanta Banerjee. I am an Asp.Net Developer. My site: http://devcorners.com/
Email: prasanta.it@hotmail.com
If any body wants to prepare for interview http://guru-code.blogspot.com/ is the good site.

Comments and Discussions

 
GeneralNothing new, all this are very original.... Pin
Md. Marufuzzaman21-Apr-11 20:34
professionalMd. Marufuzzaman21-Apr-11 20:34 
GeneralWhy have you used alter Trigger! Pin
Gandalf_TheWhite21-Apr-11 19:20
professionalGandalf_TheWhite21-Apr-11 19:20 
GeneralIt autometically handle the situation. Pin
Prasanta_Prince21-Apr-11 18:41
Prasanta_Prince21-Apr-11 18:41 
GeneralRe: you clearly don't understand SQL trigger processing, please ... Pin
SalizarMarxx22-Apr-11 0:44
SalizarMarxx22-Apr-11 0:44 
you clearly don't understand SQL trigger processing, please re-evaluate your comments.

GeneralAnd what if there are more then one row inserted? insert int... Pin
Costica U20-Apr-11 14:56
Costica U20-Apr-11 14:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.