osql -b return ERRORLEVEL but "for do ()" cleans it!

  • Hi, first post here

    if I want get and return an error from a script that call osql I can to that with the parameter -b . and then test the %errorlevel% and it works

        BUT

    if I call osql and test errorlevel in a for statement it does not works! why? examples as follows

    file to execute: prova.sql (greetings from italy!)

      use db1

      go

      select * from pippo   -- pippo is a table that does not exists

      go

      print 'this line will not be printed'

    script that works (echo return 1):

      osql -S %server% -U %myuser% -P %mypwd% -b -r 0 -e -n -i prova.sql >> %OUTPUTLOG%

      echo %errorlevel%

      if not %ERRORLEVEL% == 0 goto an_ERROR_label

    script that does not work (echo return 0 !!????)

      for  %%x in (*.sql) do (

        osql -S %SERVER% -U %UTENTE% -P %PASSWORD% -b -r 0 -e -n -i %%x >> %OUTPUTLOG%

        if not %ERRORLEVEL% == 0 goto an_ERROR_label

      )

    thank you

    bye,

    Leonardo

  • I think once that you have referenced %ErrorLevel%, it is automatically set to Zero as it is very volatile.

    Try to save %ErrorLevel% first with

    Set a = %ErrorLevel%

    echo a

    if not %ERRORLEVEL% == 0 goto an_ERROR_label


    N 56°04'39.16"
    E 12°55'05.25"

  • extremely volatile  I'have tried... without success. Another test:

    for  %%x in (*.sql) do (

      osql -S %SERVER% -U %MYUSER% -P %MYPWD% -b -e -n -i %%x >> %OUTPUTLOG%

      echo result: %ErrorLevel% >> %OUTPUTLOG%

    )

    after the call osql, echo of errorlevel returns 0...

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply