How can you continue SQL query when found error while querying?
I want to continue a query if any error will occur. I want to get the output of a certain statement and want to see if what is the last output of the query.
How can you do it in T-SQL?
SQL statements can be contained on one or more lines. To continue an SQL statement across multiple lines, the SQL statement can be split wherever a blank is allowed. The plus sign (+) can be used to indicate a continuation of a string constant. The literal continues with the first nonblank character on the next line.
When called in a CATCH block, ERROR_MESSAGE returns the complete text of the error message that caused the CATCH block to run. The text includes the values supplied for any substitutable parameters - for example, lengths, object names, or times. ERROR_MESSAGE returns NULL when called outside the scope of a CATCH block.
You can use a TRY-CATCH for error handling.
You can find enough documentation here : http://msdn.microsoft.com/en-us/library/ms175976.aspx
UPDATE:
After a bit more research I have found that using a GO
command will allow you to continue to your next query, and you won't have to restructure your entire code with TRY-CATCH statements. I still recommend using TRY-CATCH statements to control errors, but just use GO
between them.
This is the link where I found the answer: continue-executing-sql-statements-despite-errors
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With