|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionA trigger is a database object that is bound 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". When to Use TriggersThere are many reasons to use triggers. If you have a table which keeps a log of messages, you may want to have a copy of them mailed to you if they are urgent. If there were no triggers, you would have some solutions, though they are not as elegant. You could modify the application(s) logging the messages. This means that you might be redundantly coding the same thing in every application that logs messages. Tables can have multiple triggers. The SQL Server 2000 greatly enhances trigger functionality, extending the capabilities of the triggers you already know and love, and adding a whole new type of trigger, the " SQL Server 2000 has many types of triggers:
After TriggersTriggers that run after an
Note: An How to Create After Triggers
Multiple After TriggersMore than one trigger can now be defined on a table for each
This would allow you to alter triggers of one type without fear of accidentally breaking the other. If you are using multiple triggers, it is of course essential to know which order they fire in. A new stored procedure called sp_settriggerorder allows you to set a trigger to be either the "first" or "last" to fire. If you want more than two triggers to fire in a specific order, there is no way to specifically define this. A deeply unscientific test I did indicated that multiple triggers for the same table and operation will run in the order they were created unless you specifically tell them otherwise. I would not recommend relying on this though. Instead Of TriggersInstead Of Triggers fire instead of the operation that fires the trigger, so if you define an Instead Of trigger on a table for the CREATE TABLE Mayank (Name varchar(32))
GO
CREATE TRIGGER tr_mayank ON Mayank
INSTEAD OF DELETE
AS
PRINT 'Sorry - you cannot delete this data'
GO
INSERT Mayank
SELECT 'Cannot' union
SELECT 'Delete' union
SELECT 'Me'
GO
DELETE Mayank
GO
SELECT * FROM Mayank
GO
DROP TABLE Mayank
If you were to print out the contents of the inserted and deleted tables from inside an Instead Of trigger, you would see they behave in exactly the same way as normal. In this case, the deleted table holds the rows you were trying to delete, even though they will not get deleted. Instead of Triggers can be used in some very powerful ways!
If you were to define an Instead Of trigger and an After trigger on the same table for the same operation, what would happen? Because an After trigger fires after an operation completes, and an 'instead of' trigger prevents the operation from taking place, the After trigger would never fire in this situation. However, if an Instead Of trigger on a (say) delete operation contains a subsequent delete on the same table, then any After trigger defined for the delete operation on that table will fire on the basis of the This code sample creates a trigger of each type, and changed the nature of the CREATE TABLE Gupta (Comic VARCHAR (32), Preserve INT)
GO
INSERT Gupta
SELECT 'groucho', 1 UNION
SELECT 'chico', 1 UNION
SELECT 'harpo', 0 UNION
SELECT 'zeppo', 0
GO
CREATE TRIGGER trGuptaDelete ON Gupta
FOR DELETE
AS
SELECT Comic AS "deleting_these_names_only"
FROM deleted
GO
CREATE TRIGGER tr_Gupta_InsteadOf ON Gupta
INSTEAD OF DELETE
AS
DELETE Gupta
FROM Gupta
INNER JOIN Deleted
ON Gupta.Comic = Deleted.Comic
WHERE Gupta.Preserve= 0
GO
DELETE Gupta WHERE Comic IN ('GROUCHO', 'HARPO')
GO
SELECT * FROM Gupta
DROP TABLE Gupta
ImportantTriggers can be used in the following scenarios, such as: if the database is de-normalized and requires an automated way to update redundant data contained in multiple tables, or if customized messages and complex error handling are required, or if a value in one table must be validated against a non-identical value in another table. Triggers are a powerful tool that can be used to enforce the business rules automatically when the data is modified. Triggers can also be used to maintain the data integrity. But they are not to maintain data integrity. Triggers should be used to maintain the data integrity only if you are unable to enforce the data integrity using CONSTRAINTS, RULES, and DEFAULTS. Triggers cannot be created on the temporary tables.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||