PRB: Rolling Back Individual Rows from an INSERT TriggerID: Q67303
|
The following insert trigger has been defined on a table named
SALES:
CREATE TRIGGER Part_check ON SALES
FOR INSERT AS
IF NOT EXISTS (SELECT * FROM Inventory inv,inserted ins
WHERE inv.part_no = ins.part_no)
BEGIN
ROLLBACK TRANSACTION
PRINT "Part number not found in Inventory table."
PRINT "-- Transaction rolled back --"
END
INSERT Sales SELECT * FROM Returns
This problem occurs because triggers get fired only once per
transaction. For example, if a single row is inserted with the
following command, this is treated as a single transaction:
INSERT Sales VALUES (337, "Small widgets")
INSERT Sales SELECT * FROM Returns
The following trigger can be used to ensure that only the
qualifying rows get inserted:
CREATE TRIGGER Part_check ON Sales
FOR INSERT AS
DELETE Sales FROM Sales, Inserted ins
WHERE sales.part_no = ins.part_no
AND NOT EXISTS (SELECT * FROM Inventory inv
WHERE inv.part_no = ins.part_no)
Additional query words: trigger rollback transaction
Keywords : kbprg SSrvProg
Version : 4.2
Platform : OS/2
Issue type :
Last Reviewed: March 10, 1999