Click here to Skip to main content
15,118,865 members
Articles / Web Development / ASP.NET
Tip/Trick
Posted 20 Apr 2011

Stats

13.2K views
2 bookmarked

Sqlserver Trigger a quick look

Rate me:
Please Sign up or sign in to vote.
2.37/5 (11 votes)
23 Apr 2011CPOL
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)

Share

About the Author

Prasanta_Prince
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 21:34
professionalMd. Marufuzzaman21-Apr-11 21:34 
GeneralWhy have you used alter Trigger! Pin
Gandalf_TheWhite21-Apr-11 20:20
professionalGandalf_TheWhite21-Apr-11 20:20 
GeneralIt autometically handle the situation. Pin
Prasanta_Prince21-Apr-11 19:41
MemberPrasanta_Prince21-Apr-11 19:41 
GeneralRe: you clearly don't understand SQL trigger processing, please ... Pin
SalizarMarxx22-Apr-11 1:44
MemberSalizarMarxx22-Apr-11 1: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 15:56
MemberCostica U20-Apr-11 15: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.