Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 201743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:13:34+00:00 2026-05-11T17:13:34+00:00

Is there a way to immediately stop execution of a SQL script in SQL

  • 0

Is there a way to immediately stop execution of a SQL script in SQL server, like a “break” or “exit” command?

I have a script that does some validation and lookups before it starts doing inserts, and I want it to stop if any of the validations or lookups fail.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:13:34+00:00Added an answer on May 11, 2026 at 5:13 pm

    The raiserror method

    raiserror('Oh no a fatal error', 20, -1) with log
    

    This will terminate the connection, thereby stopping the rest of the script from running.

    Note that both severity level 20 or higher and the WITH LOG option are necessary for it to work this way.

    This even works with GO statements, eg.

    print 'hi'
    go
    raiserror('Oh no a fatal error', 20, -1) with log
    go
    print 'ho'
    

    Will give you the output:

    hi
    Msg 2745, Level 16, State 2, Line 1
    Process ID 51 has raised user error 50000, severity 20. SQL Server is terminating this process.
    Msg 50000, Level 20, State 1, Line 1
    Oh no a fatal error
    Msg 0, Level 20, State 0, Line 0
    A severe error occurred on the current command.  The results, if any, should be discarded.
    

    Notice that ‘ho’ is not printed.

    CAVEATS:

    • This only works if you are logged in as admin (‘sysadmin’ role), and also leaves you with no database connection.
    • If you are NOT logged in as admin, the RAISEERROR() call itself will fail and the script will continue executing.
    • When invoked with sqlcmd.exe, exit code 2745 will be reported.

    Reference: http://www.mydatabasesupport.com/forums/ms-sqlserver/174037-sql-server-2000-abort-whole-script.html#post761334

    The noexec method

    Another method that works with GO statements is set noexec on (docs). This causes the rest of the script to be skipped over. It does not terminate the connection, but you need to turn noexec off again before any commands will execute.

    Example:

    print 'hi'
    go
    
    print 'Fatal error, script will not continue!'
    set noexec on
    
    print 'ho'
    go
    
    -- last line of the script
    set noexec off -- Turn execution back on; only needed in SSMS, so as to be able 
                   -- to run this script again in the same session.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 171k
  • Answers 171k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer https://www.datasprings.com/Resources/ArticlesInformation/MigratetoDotNetNuke/tabid/737/language/en-US/Default.aspx http://forums.asp.net/t/843931.aspx The last one is a bit older -… May 12, 2026 at 2:17 pm
  • Editorial Team
    Editorial Team added an answer Use ADO.Net. Here's a link. More examples: Read excel 1st… May 12, 2026 at 2:17 pm
  • Editorial Team
    Editorial Team added an answer Before finding the edges pre-process the image with an open… May 12, 2026 at 2:17 pm

Related Questions

I'm a little confused as to what's going on, i'm playing with some programs
I have a large dataset (over 100,000 records) that I wish to load into
I'm using objective-c & the iphone 3.0 sdk I want to retrieve the contents
I'm making some changes to a legacy classic ASP application. I've made the changes

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.