Skip to content
Advertisement

Multiple batches in a SQL transaction

I want to create several stored procedures (or user-defined functions) within a transaction. CREATE PROCEDURE statement must be the only statement in the batch, so I have to use following template:

The problem appears if an error occurs within one of the batches. For example, if error occurs while procedure B is creating, MyTransaction with procedures A and B will be rolled back. But the script will continue to run. So, the procedure C will be created.

Also it’s not possible to skip the statement using GOTO and @@error counter checking, because it cannot go to a label outside the batch in which GOTO is placed.

How to prevent any database changes if an error occurs?

Advertisement

Answer

You can try to do something like this with SET XACT_ABORT ON.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement