How to exit out ofa script if it fails Halfway during execution?

  • Hi,

    I have a script worth 1000 lines which creates DB & other many objects. I am using TRY CATCH & RAISERROR in my stored proc to trap the errors.

    Problem is if it fails in between -- it does give me the error message but continues to execute after that particular GO statement and other objects are getting created. I want the script to stop right there and dont continue further. Can anyone suggest me a way to do this?

    Idont want to use Error Severity of 21 which will disconnect the connection.

    Thanks in advance...

  • in SSMS or Query analyzer, you can't...as you identified, the GO statements start a new batch, and you can't interupt the processing of the batches without raising an error severity 21 or so;

    you could wite a quick and dirty application that runs the sql file, grabbing each section delimited by GO, and stip it's execution if .ExecuteNonQuery returns an exception for example.

    but i don't think that's what you were looking for.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Heh... why can't you just remove the GO's?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply