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 9274905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:27:47+00:00 2026-06-18T16:27:47+00:00

I have the following BAT file working ok: it connects to an specific sql

  • 0

I have the following BAT file working ok:
it connects to an specific sql server and select getdate()

what I really wanted is to test whether the server is connectable.

I would like something like:

set is_connectable  = call sqlcmd.exe %%SERVITORE%%

is there any way I could achieve this?

thanks and regards
marcelo

@echo off
color fc
cls
echo.
@ECHO --- BEGIN THE SCRIPT 1 ----
echo.
@echo the SERVITORE is "%1"
echo.
echo.

if "%1"=="" GOTO USAGE


set SERVITORE=-Stcp:%1% -Q " USE MASTER select getdate() "

call sqlcmd.exe %%SERVITORE%%


color 6
GOTO THE_END

:USAGE
echo.
echo USAGE:
echo. 
ECHO the first parameter is the SERVITORE server.
echo example 1 SERVITORE108
echo.
ECHO the second parameter is optional 
echo but if not supplied the default is \\SERVITORE\folder2$
echo.

echo ATB
echo.

:THE_END
color 8
  • 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-18T16:27:48+00:00Added an answer on June 18, 2026 at 4:27 pm

    It is easy to run a query and set the result to a DOS environment variable. For instance, you can do the following to get the date/time from the SQL Server instance (SQL Server is running locally in my instance):

    for /f "skip=2 delims=" %%i in ('sqlcmd -S localhost -E -Q "set nocount on; select getdate() as [Now]"') do set is_connectable=%%i
    

    However, the is_connectable environment variable set in this example will have an arbitrary value, which will make it hard to evaluate. Since you are just trying to verify that the SQL Server is there, alive, and responsive, you should run a query that creates a more predictable output, like this:

    @echo off
    
    :: Make sure the variable is undefined to start with
    set is_connectable=
    
    :: Make the connection and run a query that should always return '1'
    for /f "skip=2 delims= " %%i in ('sqlcmd -S localhost -E -Q "set nocount on; select 1 as [Rows] into #temp; select @@rowcount as [Rows]; drop table #temp"') do set is_connectable=%%i
    
    :: Verify if SQL Server is avaialble
    if not defined is_connectable goto NotFound
    if "%is_connectable%"=="1" goto Found
    goto UnknownError
    
    :Found
    echo SQL Server was found and returned '1' as expected...
    goto TheEnd
    
    :NotFound
    echo SQL Server was not found...
    goto TheEnd
    
    :UnknownError
    echo SQLServer was found, but the return value was '%is_connectable%' instead of '1'...
    goto TheEnd
    
    :TheEnd
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following already set up and working: File file1.bat File file2.xml File
I have the following thing in my bat file. say set path=c:\temp\test so basically
I have the following problem. I got a bat file that runs testcomplete test.
I have the following code $env=array('PATH'=>'C:\Program Files\MySQL\MySQL Server 5.1\bin', 'PATHEXT' => '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC'); $cmd='mysql --port=3306
Today is 11/01/2013 I have the following .bat file: @ECHO OFF :: Take the
I have a .bat file with the following structure: istruction1[newline] I would like to
I have this test.txt file with the following content: @echo off wget -q http://subs.ro/get/21518
I have the following in a bat file : @ECHO OFF REM The following
I have the following line in a bat file: xcopy script_temp\* \\CHU-Computer-Science\CHU\scripts\ /S /E
I have the following in a BAT file: @echo off Set /P _environment =

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.