Batch file 批处理文件中的Errorlevel不工作
我正在尝试使用带有批处理脚本的sqlcmd删除数据库。我面临的问题是,如果mytest数据库在sql中不可用,那么代码就没有进入errorhandler块Batch file 批处理文件中的Errorlevel不工作,batch-file,sqlcmd,Batch File,Sqlcmd,我正在尝试使用带有批处理脚本的sqlcmd删除数据库。我面临的问题是,如果mytest数据库在sql中不可用,那么代码就没有进入errorhandler块 @echo off @echo IF ELSE DEMO sqlcmd -S <servername> -U <username> -P <password> -Q "DROP DATABASE Mytest" IF ERRORLEVEL 1 goto err_handler goto done :e
@echo off
@echo IF ELSE DEMO
sqlcmd -S <servername> -U <username> -P <password> -Q "DROP DATABASE Mytest"
IF ERRORLEVEL 1 goto err_handler
goto done
:err_handler
echo handle the error here
goto end
:done
echo script completion code here
:end
@echo %Message%
pause
我的期望是:
errorlevel
是指执行的sqlcmd
实例(应该是0
,因为它已成功执行),而不是数据库中运行的命令
另外,如前所述,回显已关闭。
消息由您的
echo %Message%
变量未定义时。该行被解析并转换为
echo
因为
%Message%
没有值。不带参数的echo
命令的行为是输出echo
状态。尝试在sqlcmd…
行之后直接添加一行echo errorlevel=%errorlevel%
,该行将显示返回的errorlevel。惯例是0=成功,但惯例有时会被打破。成功删除数据库时返回的错误级别是什么?出现回音关闭
是因为您没有为消息
分配任何内容,因此回音
将报告其状态(关闭,因为您首先执行@echo off
)-解决方案是为消息
分配一个值。感谢您的回复。它显示两种情况下的errorlevel=0(如果DB可用,如果DB不可用)
echo