Regarding Update and Insert triggers for MS SQL Server, is there a way to make them atomic? In other words, if an error occurs during the trigger, is it possible to automatically roll back the original insert or update?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
After triggers are automatically part of the insert/update/delete atomic DML statement on a table.
You simply issue ROLLBACK TRAN in the trigger to rollback all work in the trigger and the original I/U/D statement. The outermost tran is also rolled back if there is one.
Important
Rollback in a trigger for SQL 2000 and earlier aborts the batch. No code after the offending I/U/D will run. See Erland great article and another
For SQL 2005 with TRY/CATCH, execution will go to the CATCH block and your batch (aka stored proc etc) will exit normally.
See Rollbacks and Commits in Stored Procedures and Triggers. The trigger and TRY/CATCH interation is here