Triggers in SQL Server
- Normally the Triggers are special kind of stored procedure, it will fire when some event occured in the sQL object like Table, View.
- The triggers will fire before/After Insert, UPdate, Delete operations made on the Talbes or Views.
- Those Trigger is Classified into 2 categories.
1. After Triggers and
2. Instead Triggers.
1. After(For) Triggers:
The After triggers are excuted the After the above set operations(eigther Insert, Upadate or Delete)
Example:
--After Insert and After Update
CREATE TRIGGER ToAlert
ON M_Customer
FOR INSERT -- (OR)
FOR UPDATE
AS
SELECT 'You Have Inserted a New Record!'
GO
-- After Delete Tigger
CREATE TRIGGER ToDelAlert
ON M_Customer
AFTER DELETE
AS
SELECT 'You have Deleted a Records!'
GO
2. Instead Of Triggers
The instead of triggers are fired before the Operation what we given for the triggers (i.e., Insert, Update, Delete)
-- INSTED OF UPDATE/INSERT
CREATE TRIGGER ToAlert
ON M_Customer
INSTEAD OF INSERT -- (OR)
INSTEAD OF UPDATE
AS
SELECT 'You going to Make Insert a New Record or Update Operation! '
GO
-- INSTEAD DELETE
CREATE TRIGGER ToDelAlert
ON M_Customer
INSTEAD OF DELETE
AS
SELECT 'Are you going Delete a Records!'
GO
- Normally triggers are like a events in a programming language.
- We unable to call explicitly, this will fire while the event occured for the DB objects which its assigned for the particular operations.
- We unable to use triggers for Truncate Table, DROP table sql commands.
Triggers And it's Type
Subscribe to:
Post Comments (Atom)



0 comments:
Post a Comment