Is there a way to stop executing code once I ROLLBACK a transaction in T-SQL? For example, in the code below I want ‘Message 1’ to print, but not ‘Message 2’.
BEGIN TRANSACTION
GO
PRINT 'Message 1'
ROLLBACK TRANSACTION
GO
PRINT 'Message 2'
GO
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.
The
GOstatement is separating the batches, this means that even if the first one errors, the next batch will run. I’m assuming (and you know what that means…) that you’re trying to circumvent if you’ve got an error.You can look at
GOTO, and have a error handling block. Or you can just have aRETURN; however, this needs to be within the sameGOblockExample:
will still return Test 2, but not test 1.