Triggers in sql server
Definition:
Are special type of stored procedure that are defined to execute automatically, In place of or after data modification when Insert, Update, Delete triggering actions occurred on that table.
1) After Trigger
Fired the triggering action.
Executed automatically before the transaction is committed or rolled back.
e.g.
CREATE TRIGGER trgCheckStock ON [products]
FOR UPDATE
AS
IF( Select inStockFrom inserted) < 0)
BEGIN
PRINT 'Can not oversell products'
PRINT 'Transaction has been canceled'
END
GO
2) Instead Of Trigger
Fire in place of the triggering action.
Executed automatically before primary key and foreign key constraints are checked.
e.g.
CREATE TRIGGER trgCantDelete ON table1
INSTEAD OF DELETE
AS
PRINT 'you cannot delete this data'
GO
Comments
Post a Comment