Return value of SQLCMD

后端 未结 2 2007
轻奢々
轻奢々 2021-01-01 15:48

I need to check the exit status (success/failure) of a query run through SQLCMD utility. For example, the server I am connecting doesn\'t have a database name

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-01 16:12

    You need to use the -V option

    Note: That's a capital -V, not a lowercase -v.

    Example:

    > SQLCMD.EXE -S whatever -E -V16 -Q "USE does_not_exist"
      Msg 911, Level 16, State 1, ...
      Could not locate entry ...
    > echo %ERRORLEVEL%
      16
    

    Update: Alternatively, you can use the -b option, which has different semantics to the execution (the whole batch stops on the first error). YMMV.

    Example:

    > SQLCMD.EXE -S whatever -E -b -Q "USE does_not_exist"
      Msg 911, Level 16, State 1, ...
      Could not locate entry ...
    > echo %ERRORLEVEL%
      1
    

    You can also combine -b and -V.

提交回复
热议问题