Triggers
A trigger is a special kind of stored procedure that automatically
executes when an event occurs in the database server. DML triggers
execute when a user tries to modify data through a data manipulation
language (DML) event. DML events are INSERT, UPDATE, or DELETE
statements on a table or view. These triggers fire when any valid event
is fired, regardless of whether or not any table rows are affected.
CREATE TRIGGER trgInsert ON [dbo].[Employee_Test] FOR INSERT AS declare @empid int; declare @empname varchar(100); declare @empsal decimal(10,2); declare @audit_action varchar(100); select @empid=i.Emp_ID from inserted i; select @empname=i.Emp_Name from inserted i; select @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.' GO
The CREATE TRIGGER
statement is used to create the trigger.
THE ON
clause specifies the table name on which the trigger is to be attached.
The FOR INSERT
specifies that this is an AFTER INSERT
trigger.
In place of FOR INSERT
, AFTER INSERT
can be used
Comments
Post a Comment