Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: If statements in command line  (Read 3426 times)

0 Members and 1 Guest are viewing this topic.

priyabala

    Topic Starter


    Greenhorn

    If statements in command line
    « 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.

     

    Salmon Trout

    • Guest
    Re: If statements in command line
    « Reply #1 on: September 20, 2010, 02:03:53 PM »
    Where does the variable %BATCH_QUERY% get a value?

    priyabala

      Topic Starter


      Greenhorn

      Re: If statements in command line
      « Reply #2 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)

      Salmon Trout

      • Guest
      Re: If statements in command line
      « Reply #3 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).





      priyabala

        Topic Starter


        Greenhorn

        Re: If statements in command line
        « Reply #4 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

        Salmon Trout

        • Guest
        Re: If statements in command line
        « Reply #5 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?


        priyabala

          Topic Starter


          Greenhorn

          Re: If statements in command line
          « Reply #6 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.

          Salmon Trout

          • Guest
          Re: If statements in command line
          « Reply #7 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.




          priyabala

            Topic Starter


            Greenhorn

            Re: If statements in command line
            « Reply #8 on: September 21, 2010, 02:10:40 PM »
            Thanks for all your replies.I'll try these.

            ALAN_BR



              Hopeful

              Thanked: 5
              • Computer: Specs
              • Experience: Experienced
              • OS: Windows 7
              Re: If statements in command line
              « Reply #9 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