question

sqlrookie avatar image
sqlrookie asked

How to stop the execution of script when error.

Hello, I need some suggestions about stopping the execution of a script when an error occurs. Let's say we have several blocks of codes and these blocks are try catch blocks. I want my script to execute in such a way that when an error occurs in say 6th block, the execution should stop there and throw an error. It should not rollback 5th,4th,3rd,2nd and 1st block, but should rollback the 6th block of codes. I am using sql server 2008 express edition. Your help will be appreciated.
executecode
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
sqlrookie avatar image
sqlrookie answered
I came to know the answer for it. Using RETURN would exit or break the execution when error occurs in the script. begin try; begin tran; -- enter code end try begin catch print error_message() RETURN end try
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
Nice. Go ahead an mark your answer by clicking on the check mark.
1 Like 1 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.