How to Stop a Batch in SQL Query Analyzer

I was compiling a release script the other day, and needed to check if all data items in a column were unique before adding a Unique constraint to a table. I wanted a way to stop Query Analyzer (QA) from executing the batch if the check revealed there were duplicate items in the column. I tried a few obvious things like Return and raiserror but these didn’t work. I then realised that the issue lay with the GO command. I found this in BOL “GO is not a Transact-SQL statement; it is a command recognized by the osql and isql utilities and SQL Query Analyzer.” unfortunately there isn’t a corresponding STOP command so you cannot easily perform a test in T-SQL and then tell Query Analyzer (QA) to stop processing the rest of the batch.

I did however find a dirty way around this problem. Using a While loop and the Print command I was able to repeatedly display a message to the user explaining the problem and instructing them to cancel the execution. Not very elegant but it did work.

BOL Search Terms: Batches, Batch Processing

4 thoughts on “How to Stop a Batch in SQL Query Analyzer”

  1. hi.Charlie,
    I had meet the same problem
    I have many batch Will run in mssql Query Analyzer .
    if a error happen in a batch. other batch run continue.
    but i hope exit all batch.
    if you have a answer.can you tell me?
    thanks a lot.

Comments are closed.