1.

Solve : If statements in command line?

Answer» <html><body><p>Hi,<br/><br/>   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 <a href="https://interviewquestions.tuteehub.com/tag/control-425557" style="font-weight:bold;" target="_blank" title="Click to know more about CONTROL">CONTROL</a> always goes to LAST STEP.<br/><br/>Also can you please explain how to do if&lt;&gt; and &lt;&gt;<br/><br/>Thanks and appreciate your help on this.<br/><br/>Here is the code:<br/><br/>ECHO ON<br/><br/>SET JOBNAME=TEST<br/><br/>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)<br/><br/><br/>if %ERRORLEVEL% equ 0 if %BATCH_QUERY% gtr 0 (GOTO LASTSTEP)<br/>if %ERRORLEVEL% NEQ 0 (GOTO ERROR1)<br/><br/>:LASTSTEP<br/>echo ********************************************** <br/>echo * <a href="https://interviewquestions.tuteehub.com/tag/completion-926298" style="font-weight:bold;" target="_blank" title="Click to know more about COMPLETION">COMPLETION</a> OK                        *<br/>echo ********************************************** <br/>:NORMAL<br/>ECHO <a href="https://interviewquestions.tuteehub.com/tag/sucessful-3085234" style="font-weight:bold;" target="_blank" title="Click to know more about SUCESSFUL">SUCESSFUL</a> Completion of ICV001. <br/>SET ControlM=0<br/>GOTO END<br/><br/><br/>:ERROR1<br/>echo ********************************************** <br/>echo * %JOBNAME% NOT OK                       *<br/>echo ********************************************** <br/>echo %JOBNAME% Abended<br/>REM THERE WAS AN ERROR!<br/>SET CONTROLM=1<br/>goto END<br/><br/><br/>:END<br/>ECHO %JOBNAME% done. <br/><br/>  Where does the variable %BATCH_QUERY% get a value?<br/>from %%G after the query executes.<br/><br/>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)<br/> Quote</p><blockquote>from %%G after the query executes.</blockquote> <br/>That does not set the value; it echoes %BATCH_QUERY%, a colon, and <a href="https://interviewquestions.tuteehub.com/tag/whatever-736216" style="font-weight:bold;" target="_blank" title="Click to know more about WHATEVER">WHATEVER</a> %%G happens to be. If you did not do <br/><br/>SET BATCH_QUERY=<br/><br/>before, it will be undefined (blank).<br/><br/><br/><br/><br/>So,should it be like this......<br/><br/>set BATCH_QUERY=0<br/><br/>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<br/> Quote from: priyabala on September 20, 2010, 02:58:22 PM<blockquote>So,should it be like this......<br/><br/>set set BATCH_QUERY=0<br/><br/>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<br/><br/></blockquote> <br/>If you want the output of the sqlcmd command to be assigned to the variable BATCH_QUERY you could do something like this<br/><br/> Code: <a>[Select]</a>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<br/>is it going to be a number?<br/><br/>Sorry about the delayed reply.<br/><br/>Yes, it is going to be a number.<br/><br/>Also,can you please let me know how to do multiple conditions in if statments.Is this correct.<br/><br/>if %ERRORLEVEL% equ 0 if %BATCH_QUERY% gtr 0 (GOTO LASTSTEP)<br/>if %ERRORLEVEL% NEQ 0 (GOTO ERROR1)<br/><br/><br/>Please let me know.I really appreciate your help on this.<br/><br/>Thanks.<br/> Code: <a>[Select]</a><br/>REM put this test FIRST<br/>if %errorlevel% neq 0 goto error1<br/><br/>REM You will only get here if errorlevel is 0<br/>if %batch_query% gtr 0 goto laststep<br/><br/>REM If you get here errorlevel is 0 and batch_query is 0<br/>REM What are you going to do?<br/>REM Maybe goto end?<br/>REM If you do nothing you will go to the next line.<br/>REM Which is laststep...<br/><br/>:laststep<br/>REM Your code<br/>goto end<br/><br/>:error1<br/>REM your code<br/><br/>:end<br/><br/><br/>Also... in your code...<br/><br/> Code: <a>[Select]</a>echo %JOBNAME% Abended<br/>REM THERE WAS AN ERROR!<br/>SET CONTROLM=1<br/>goto END<br/><br/><br/>:END<br/>1. The goto END statement is superfluous, because you are going there anyway.<br/><br/>2. "Abended" is not an <a href="https://interviewquestions.tuteehub.com/tag/english-273" style="font-weight:bold;" target="_blank" title="Click to know more about ENGLISH">ENGLISH</a> word.<br/><br/><br/><br/>Thanks for all your replies.I'll try these.I have always found life easier if a script commences<br/>ECHO OFF<br/>or even<br/>ECHO OFF<br/><br/>I rather doubt that there is any benefit from starting with<br/>ECHO ON<br/><br/>Alan<br/></body></html>


Discussion

No Comment Found