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

  • Home
  • SEARCH
  • 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 8292703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:34:24+00:00 2026-06-08T13:34:24+00:00

I am calling sqlcmd from PowerShell to execute a T-SQL script. Currently I am

  • 0

I am calling sqlcmd from PowerShell to execute a T-SQL script. Currently I am using “:On Error exit” to exit the script if there is an error caused by the data used violating a constraint etc. This is handled by PowerShell detecting the $SqlcmdProcess.ExitCode of 1.

However, if there is a connectivity issue with the database, sqlcmd also gives an ExitCode of 1. Is there a way to set the :On Error ExitCode to something other than 1? I’m aware of using something like :Exit(SELECT 2) to do this, but I’d rather still use :On Error so I don’t have to rewrite the script.

  • 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-06-08T13:34:25+00:00Added an answer on June 8, 2026 at 1:34 pm

    You could use the exit keyword in Powershell. Here’s an example

    Create a script called sqlcmdexit.ps1, with something like the following:

    $result = sqlcmd -S"missing" -d master -Q "select @@servername"
    if ($result[1] -like "*Error Locating Server/Instance Specified*" -or $result[1] -like "*Could not open a connection to SQL Server*") {
        exit 99
    }
    

    Call script and observe exist code:

    C:\Users\Public\bin>.\sqlcmdExit.ps1
    Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : A network-related or instance-specific error has occurred while
     establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and i
    f SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
    Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : Login timeout expired.
    
    C:\Users\Public\bin>$LASTEXITCODE
    99
    

    I’m not aware of any way to set default ExitCode. Using start-process you could something similar:

    $tempFile = [io.path]::GetTempFileName()
    $exitCode = (Start-Process -FilePath "sqlcmd.exe" -ArgumentList @"
    -S"missing" -d master -Q "select @@servername"
    "@ -Wait -NoNewWindow -RedirectStandardOutput $tempFile -Passthru).ExitCode
    
    if ($exitCode -eq 1) {
        $result = get-content $tempfile
        if ($result[1] -like "*Error Locating Server/Instance Specified*" -or $result[1] -like "*Could not open a connection to SQL Server*") {
            remove-item $tempFile
            Exit 99
        }
    }
    else {
        remove-item $tempfile
        Exit $exitCode
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a powershell script which will execute sqlcmd.exe to run a
I have a sql script say abc.sql which I am calling from a batch
Calling the WriteObject Method from a background thread is not possible! Is there a
Calling a .net SOAP1.1 web service from android using ksoap2 lib I met a
Calling a Lua function from C is fairly straight forward but is there a
Calling this powershell command and getting an error. Driving me nuts. Prompt> get-childitem -recurse
Calling image = Image.open(data) image.thumbnail((36,36), Image.NEAREST) will maintain the aspect ratio. But I need
Calling Html.RenderPartial(~/Views/Payments/MyControl.ascx); from a view works if MyControl.ascx is a control that directly inherits
Calling the ajax called URL works well without ajax eg. http://localhost/ci/controller/method/param_value . But using
Calling urrlib2.urlopen on a link to an article fetched from an RSS feed leads

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.