I have a large generated script. It has multiple batches (GO statements). The first line is BEGIN TRAN
If any errors occur during execution, either raised myself using RAISERROR, or raised by SQL Server, I want to rollback the transaction. I want the script to continue executing until the very end, then rollback, not abort execution as soon as any error occurs.
Checking @@error <> 0 at the end of the script doesn’t seem to be sufficient, because if the last statement in the script succeeds, @@error will be 0, even though previous statements failed.
I can’t declare a @rollback BIT at the very beginning because the script is segmented into multiple batches, and so the variable is out of scope.
I understand that RAISERROR doesn’t mean my transaction will be rolled back.
Any suggestions on the best way to implement this in a SQL 2000 compatible manner?
You can always use
SET CONTEXT_INFOto pass ‘out-of-band’ information, and read it later usingCONTEXT_INFO(). If an error occurs, set the context info to a value which you check just before commit.This answers your question of how to do it, but I really have to express my doubts about why do it in the first place. For one many errors will abort the transaction anyway so you may blindly proceed believing that is safe to continue and rollback only to discover that there is nothing to rollback and all your erroneous script after the exception has committed. Second, for the exceptions than do not abort the transaction, is still very questionable to do any work only to roll it back. Your request is so out of the beaten path than I must wonder if you really understand all the implications of what you’re doing.