Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Sqlserver Trigger a quick look

2.37/5 (11 votes)
23 Apr 2011CPOL 15.9K  
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)