locked
ErrorLevel and Exit not working RRS feed

  • Question

  • Hello,

    As  am looping with this batch I will ask why the following:

    sqlcmd -S ServerName\InstanceName -Q "exit(select (is_read_only | state) from sys.databases where name = 'SRRS')"
    IF (0==%errorlevel%) (
     echo Server is Active - Running backup 
     ) ELSE ( 
     echo Server is Mirror - Don't Bother 

    is giving me the following results...

    C:\Users\Documents>sqlcmd -S ServerName\InstanceName -Q "exit(select (is_read_
    only | state) from sys.databases where name = 'SRRS')"
    ---
      0
    (1 rows affected)
    C:\Users\Documents>IF (0 == 0) (echo Server is Active - Running backup  )
      ELSE (echo Server is Mirror - Don't Bother  )
    Server is Mirror - Don't Bother

    or

    C:\Users\Documents>sqlcmd -S ServerName\InstanceName -Q "exit(select (is_read_
    only | state) from sys.databases where name = 'SRRS')"
    ---
      1
    (1 rows affected)
    C:\Users\Documents>IF (0 == 1) (echo Server is Active - Running backup  )
      ELSE (echo Server is Mirror - Don't Bother  )
    Server is Mirror - Don't Bother

    which are identical even the %ErrrorLevel% is different... I was expecting something like

     for ErrorLevel = 1 ==> echo Server is Active - Running backup  
     for ErrorLevel = 0 ==> Server is Mirror - Don't Bother

    but not all the script and the same results for both case

    Any idea?

    Thanks,

    Dom


    System Center Operations Manager 2007 / System Center Configuration Manager 2007 R2 / Forefront Client Security / Forefront Identity Manager


    • Edited by Felyjos Wednesday, December 30, 2015 1:26 AM
    Wednesday, December 30, 2015 1:24 AM

Answers

  • Adding

    Echo off helps now I will try to parameterize the server names as it is a mirror environment...

    Thanks,

    Dom


    System Center Operations Manager 2007 / System Center Configuration Manager 2007 R2 / Forefront Client Security / Forefront Identity Manager

    • Marked as answer by Felyjos Wednesday, December 30, 2015 1:55 AM
    Wednesday, December 30, 2015 1:55 AM