Wednesday, December 2, 2009

After /for Trigger example Using Sqlserver2005

 A trigger is a database object that is attached to a table. In many aspects it is similar to a stored procedure. As a matter of fact, triggers are often referred to as a "special kind of stored procedure." The main difference between a trigger and a stored procedure is that the former is attached to a table and is only fired when an INSERT, UPDATE or DELETE occurs. You specify the modification action(s) that fire the trigger when it is created.
The following shows how to create a trigger that displays the current system time when a row is inserted into the table to which it is attached.


create trigger trg2 on pro for
insert 
as
declare @pno int;
declare @productname varchar(50);
declare @cost varchar(50);
declare @action1 varchar(100);

select @pno =i.pno from inserted i;
select @productname =i.productname from inserted i;
select @cost =i.cost from inserted i;
select @action1 = 'inserted record after insert trigger'
insert into protrigger(pno, productname, cost, action1, time1)values (@pno, @productname, @cost, @action1, getdate());
print'after insert Trigger find';
go