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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
snomad
Posts: 3
Joined: 18 Nov 2011 03:18

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

#1 Post by snomad » 18 Nov 2011 03:23

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.

OJBakker
Expert
Posts: 39
Joined: 12 Aug 2011 13:57

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

#2 Post by OJBakker » 18 Nov 2011 03:59

use the exit command
exit /b 0 or exit /b 1

snomad
Posts: 3
Joined: 18 Nov 2011 03:18

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

#3 Post by snomad » 18 Nov 2011 04:10

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.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

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

#4 Post by Ed Dyreen » 18 Nov 2011 04:30

'

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%

snomad
Posts: 3
Joined: 18 Nov 2011 03:18

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

#5 Post by snomad » 18 Nov 2011 05:27

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 ....

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

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

#6 Post by Ed Dyreen » 18 Nov 2011 06:13

'

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%

Post Reply