TRY/CATCH blocks can only be used inside the scope a single request (batch). You are trying to span a TRY block over several batches. GO
is the default batch delimiter, fyi.
You also expect to support transactions and rollback for some operations that do not support transactions, like RESTORE
:
RESTORE is not allowed in an explicit or implicit transaction.
You would have a much better chance at handling this kind of problems at a higher level, in an app or even is a script. Note that sqlcmd
supports the -b
option to break on first error, and you can use the exit code (%ERRORLEVEL%
).