-
Erland Sommarskog 78,826 Reputation points MVP
2022-04-07T22:04:16.293+00:00 If you want to do this as an INSTEAD OF trigger, the INSERT trigger should look like this outline:
CREATE TRIGGER MyTrigger ON MyTable INSTEAD OF INSERT AS IF NOT EXISTS (SELECT * FROM inserted) RETURN IF <bad condition is true> BEGIN RAISERROR('Blue murder!', 16, 1) ROLLBACK TRANSACTION RETURN END INSERT tbl (col1, col2, col3, ....) SELECT col1, co2, col3, ... FROM inserted
Since repeating the INSERT statement is kind of boring and opens for a maintenance problem, most people do this as an AFTER trigger. But if you expect the bad condition to appear often, it cannot be denied that the INSTEAD OF trigger is more efficient.
Note: Never do COMMIT TRANSACTION in a trigger, that will not end well!
Sure you can create such an trigger, and "Reject" would be a rollback.
Keep in mind, that a trigger fires once per transaction, not for each effected recordset and a ROLLBACK here means rollback the complete transaction.
Better create procedure(s) which checks the data first before you INSERT/UPDATE.