Page 1 of 1

Dos batch to return 1 or 0 exit code after SQL query.

Posted: 18 Nov 2011 03:23
by snomad
I know this is basic stuff but I just can't figure it out. :oops:

I need to do a sql query & depending on the outcome return an exit code of 1 or 0.

It's to determine which is the principal database in a mirror pair & therefore which database should be backed up by my backup app.
So the backup app runs the batch ... which runs a sqlcmd ... which returns a yes or no.
How do I return this as an exit code to the app.

I know how to output a 0 or 1 to a text file but can't suss how to return an exit code.

Grateful for any help at all!!!

Thanks.

Re: Dos batch to return 1 or 0 exit code after SQL query.

Posted: 18 Nov 2011 03:59
by OJBakker
use the exit command
exit /b 0 or exit /b 1

Re: Dos batch to return 1 or 0 exit code after SQL query.

Posted: 18 Nov 2011 04:10
by snomad
Thanks ... is it possible to test this.

ie.
Batch 1 calls batch 2
Batch 2 runs sql & exits with the 0 or 1
Batch 1 returns something / does something with the exit code.

Sorry if these are totally dumb questions. Appreciate your time.

Re: Dos batch to return 1 or 0 exit code after SQL query.

Posted: 18 Nov 2011 04:30
by Ed Dyreen
'

Code: Select all

@echo off

call :func "()" %= or a batch file =%
echo.$err=%$err%_
if %$err% neq 0 echo.$err is set.
pause
exit /b %$err%

:func () %= or a batch file =%
::(
  if 1 equ 0 set /a $err = 1 else set /a $err = 0
::)
exit /b %$err%

Re: Dos batch to return 1 or 0 exit code after SQL query.

Posted: 18 Nov 2011 05:27
by snomad
Thanks for the replies. I don't really understand the last one but I've made progress with your help ...

The final bit I'm stuck on is:
sql query returns 0 or 1
how do I convert this into my exit /b 1 or 0?

if ... but can't suss the syntax. how do i say if ... incoming value from batch 1 ....

Re: Dos batch to return 1 or 0 exit code after SQL query.

Posted: 18 Nov 2011 06:13
by Ed Dyreen
'

Code: Select all

@echo off

call sqlbatch.CMD
set /a $err = %errorlevel%
echo.$err=%$err%_
pause
exit /b %$err%
sqlbatch.CMD

Code: Select all

sql.....
set /a $err = %errorlevel%
echo.$err=%$err%_
exit /b %$err%