Computer Hope
Microsoft => Microsoft DOS => Topic started 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.
-
Where does the variable %BATCH_QUERY% get a value?
-
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)
-
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).
-
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
-
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
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?
-
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.
-
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...
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.
-
Thanks for all your replies.I'll try these.
-
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