sql-server – SQLCMD的返回值

我需要检查通过sqlCMD实用程序运行的查询退出状态(成功/失败).例如,我正在连接的服务器没有数据库名称Eastwind.然后,以下命令失败,并显示消息…
> "C:\Program Files\Microsoft sql Server\100\Tools\Binn\sqlCMD.EXE" 
     -S ZEPHIR -E -Q "USE Westwind"
Changed database context to 'Westwind'.
> echo %errorlevel%
0
> "C:\Program Files\Microsoft sql Server\100\Tools\Binn\sqlCMD.EXE" 
     -S ZEPHIR -E -Q "USE Eastwind"
Database 'Eastwind' does not exist. Make sure that the name is entered correctly
> echo %errorlevel%
0

我看到这两种情况下的返回值是一样的.如何在sqlCMD中检查命令是否失败?

解决方法

你需要使用 -V option.

例:

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

更新:或者,您也可以使用-b选项.其具有不同的语义到执行(整个批次停止在第一个错误).因人而异.

例:

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

您也可以组合-b和-V.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...