Sql server trigger|Update trigger|SQL Server trigger example

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

triggers are classified into two main types:

1. After Triggers (For Triggers)
2. Instead Of Triggers

Example:
Create TRIGGER TransBalanceDet ON [dbo].[Trans_Balance_Det] 
FOR UPDATE
AS
 declare @Closed int;
 declare @Balance int;
 
 select @Closed =i.Closed  from inserted i; 
 select @Balance=i.Balance  from inserted i; 
 
 
 if (@Closed=1)
 BEGIN
    IF (@Closed=1 AND @Balance<>0)
    BEGIN
       RAISERROR('Record(s) are not allowed to closed',16,1);
       ROLLBACK;
 END
END

Post a Comment

0 Comments