Computer Hope

Microsoft => Microsoft DOS => Topic started by: priyabala on September 20, 2010, 12:07:19 PM

Title: If statements in command line
Post by: priyabala on September 20, 2010, 12:07:19 PM
Hi,

   I'm trying to create a command script(.cmd file) with if statements.I don't think the if statements work here.Because even though the first if condition fails,the control always goes to LAST STEP.

Also can you please explain how to do if<<condition>> and <<condition>>

Thanks and appreciate your help on this.

Here is the code:

ECHO ON

SET JOBNAME=TEST

for /F  %%G in ('sqlcmd -E -S TAMANS-SQ12DCL\SQL1A -d SalesInterface -h-1 -W -Q "set nocount on;SELECT count(*) FROM table_test"') do (@echo %BATCH_QUERY%:%%G)


if %ERRORLEVEL% equ 0 if %BATCH_QUERY% gtr 0 (GOTO LASTSTEP)
if %ERRORLEVEL% NEQ 0 (GOTO ERROR1)

:LASTSTEP
echo **********************************************
echo * COMPLETION OK                        *
echo **********************************************
:NORMAL
ECHO Sucessful Completion of ICV001.
SET ControlM=0
GOTO END


:ERROR1
echo **********************************************
echo * %JOBNAME% NOT OK                       *
echo **********************************************
echo %JOBNAME% Abended
REM THERE WAS AN ERROR!
SET CONTROLM=1
goto END


:END
ECHO %JOBNAME% done.

 
Title: Re: If statements in command line
Post by: Salmon Trout on September 20, 2010, 02:03:53 PM
Where does the variable %BATCH_QUERY% get a value?
Title: Re: If statements in command line
Post by: priyabala on September 20, 2010, 02:32:34 PM
from %%G after the query executes.

for /F  %%G in ('sqlcmd -E -S TAMANS-SQ12DCL\SQL1A -d SalesInterface -h-1 -W -Q "set nocount on;SELECT count(*) FROM table_test"') do (@echo %BATCH_QUERY%:%%G)
Title: Re: If statements in command line
Post by: Salmon Trout on September 20, 2010, 02:51:31 PM
Quote
from %%G after the query executes.

That does not set the value; it echoes %BATCH_QUERY%, a colon, and whatever %%G happens to be. If you did not do

SET BATCH_QUERY=

before, it will be undefined (blank).




Title: Re: If statements in command line
Post by: priyabala on September 20, 2010, 02:58:22 PM
So,should it be like this......

set BATCH_QUERY=0

set BATCH_QUERY=for /F  %%G in ('sqlcmd -E -S test-SQ17CL\SQL1A -d SalesInterface -h-1 -W -Q "set nocount on;SELECT count(*) FROM table_test"') do @echo %%G
Title: Re: If statements in command line
Post by: Salmon Trout on September 20, 2010, 03:35:56 PM
So,should it be like this......

set set BATCH_QUERY=0

set BATCH_QUERY=for /F  %%G in ('sqlcmd -E -S test-SQ17CL\SQL1A -d SalesInterface -h-1 -W -Q "set nocount on;SELECT count(*) FROM table_test"') do @echo %%G


If you want the output of the sqlcmd command to be assigned to the variable BATCH_QUERY you could do something like this

Code: [Select]
for /F  %%G in ('sqlcmd -E -S test-SQ17CL\SQL1A -d SalesInterface -h-1 -W -Q "set nocount on;SELECT count(*) FROM table_test"') do set BATCH_QUERY=%%G
is it going to be a number?

Title: Re: If statements in command line
Post by: priyabala on September 21, 2010, 07:18:13 AM
Sorry about the delayed reply.

Yes, it is going to be a number.

Also,can you please let me know how to do multiple conditions in if statments.Is this correct.

if %ERRORLEVEL% equ 0 if %BATCH_QUERY% gtr 0 (GOTO LASTSTEP)
if %ERRORLEVEL% NEQ 0 (GOTO ERROR1)


Please let me know.I really appreciate your help on this.

Thanks.
Title: Re: If statements in command line
Post by: Salmon Trout on September 21, 2010, 11:39:05 AM
Code: [Select]

REM put this test FIRST
if %errorlevel% neq 0 goto error1

REM You will only get here if errorlevel is 0
if %batch_query% gtr 0 goto laststep

REM If you get here errorlevel is 0 and batch_query is 0
REM What are you going to do?
REM Maybe goto end?
REM If you do nothing you will go to the next line.
REM Which is laststep...

:laststep
REM Your code
goto end

:error1
REM your code

:end


Also... in your code...

Code: [Select]
echo %JOBNAME% Abended
REM THERE WAS AN ERROR!
SET CONTROLM=1
goto END


:END

1. The goto END statement is superfluous, because you are going there anyway.

2. "Abended" is not an English word.



Title: Re: If statements in command line
Post by: priyabala on September 21, 2010, 02:10:40 PM
Thanks for all your replies.I'll try these.
Title: Re: If statements in command line
Post by: ALAN_BR on September 22, 2010, 09:30:29 AM
I have always found life easier if a script commences
@ECHO OFF
or even
ECHO OFF

I rather doubt that there is any benefit from starting with
ECHO ON

Alan