10

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?

6
  • 5
    You can't continue a query and ignore all errors. Assuming you mean that you would want a result set with 1,2 from the following SELECT * FROM (SELECT 1 UNION ALL SELECT CAST('X' AS INT) UNION ALL SELECT 2) T(C) Commented Jun 20, 2013 at 9:27
  • @MartinSmith Is TRY CATCH useful in my case? Commented Jun 20, 2013 at 9:51
  • 3
    What is your case? If it is as per my earlier comment then no that won't do what you want. Can you be more specific about what you are trying to do? Commented Jun 20, 2013 at 9:52
  • 2
    When you say query, do you mean a single statement (like a single SELECT or a single INSERT or single UPDATE etc.) or a batch (as in multi-statement query)? Commented Jun 21, 2013 at 7:02
  • 2
    What is your case? What SQL do you have that you want to "continue" in case of an error? Show us a piece of code, don't leave us hanging. Commented Jun 21, 2013 at 7:09

1 Answer 1

13

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

Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't really explain how one can have the SQL query continue when using TRY...CATCH.
TRY CATCH only allows you to control error handling. Not continue.
This helped. I paired GO with the running of a 118mb script on SQLCMD -d <Databasename> -i <instancename> . turns out it was a single, annoying, little, enfuriating change one of the developers made causing one of my values to not find a foreign key :)
@Noobgrammer Glad it was useful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.