批处理文件中的错误级别不起作用 [英] Errorlevel in batch file not working

查看:166
本文介绍了批处理文件中的错误级别不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有批处理脚本的sqlcmd删除数据库.我面临的问题是,如果mytest数据库在sql中不可用,则代码不会进入错误处理程序块.

I'm trying to delete the database using sqlcmd with a batch script. The problem I was facing is the code isnot entering the errorhandler block if mytest database is not available in the sql.

@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

并提供以下输出:

IF ELSE DEMO
Msg 3701, Level 11, State 1, Server HERCULES, Line 1
Cannot drop the database 'Mytest', because it does not exist or you do not have permission.
script completion code here
ECHO is off.
Press any key to continue . . .

我的期望是:

  1. 如果数据库可用,则应删除数据库,并输入:done代码;如果数据库不可用,则应输入:err_handler块.

  1. if the database is available it should delete the database and should enter into :done code if the database is not available it should enter into :err_handler block.

我不希望Echo关闭消息.

I don't want Echo is off message.

推荐答案

来自 MS文档:

错误报告选项

Error Reporting Options

-b

指定sqlcmd退出并在出现以下情况时返回DOS ERRORLEVEL值 发生错误.返回到DOS错误级别的值 SQL Server错误消息具有严重性级别时,变量为1 大于10;否则,返回的值为0. 除了-b以外,还设置了sqlcmd,如果 严重性级别低于使用-V设置的值.命令提示符 批处理文件可以测试ERRORLEVEL的值并处理错误 适当地. sqlcmd不报告严重性级别10的错误 (信息性消息).

Specifies that sqlcmd exits and returns a DOS ERRORLEVEL value when an error occurs. The value that is returned to the DOS ERRORLEVEL variable is 1 when the SQL Server error message has a severity level greater than 10; otherwise, the value returned is 0. If the -V option has been set in addition to -b, sqlcmd will not report an error if the severity level is lower than the values set using -V. Command prompt batch files can test the value of ERRORLEVEL and handle the error appropriately. sqlcmd does not report errors for severity level 10 (informational messages).

如果sqlcmd脚本包含不正确的注释,语法错误或 缺少脚本变量,返回的ERRORLEVEL为1.

If the sqlcmd script contains an incorrect comment, syntax error, or is missing a scripting variable, ERRORLEVEL returned is 1.

-m error_level

控制将哪些错误消息发送到stdout.具有以下内容的消息 严重性级别大于或等于此级别. 当此值设置为-1时,包括信息性消息在内的所有消息 消息,已发送. -m和-1之间不允许有空格.为了 例如,-m-1有效,而-m -1无效.

Controls which error messages are sent to stdout. Messages that have a severity level greater than or equal to this level are sent. When this value is set to -1, all messages including informational messages, are sent. Spaces are not allowed between the -m and -1. For example, -m-1 is valid, and -m -1 is not.

此选项还设置sqlcmd脚本变量SQLCMDERRORLEVEL. 此变量的默认值为0.

This option also sets the sqlcmd scripting variable SQLCMDERRORLEVEL. This variable has a default of 0.

-V error_severity_level

控制用于设置ERRORLEVEL的严重性级别 多变的.严重级别大于或的错误消息 等于此值设置ERRORLEVEL.小于0的值是 报告为0.批处理和CMD文件可用于测试值 ERRORLEVEL变量.

Controls the severity level that is used to set the ERRORLEVEL variable. Error messages that have severity levels greater than or equal to this value set ERRORLEVEL. Values that are less than 0 are reported as 0. Batch and CMD files can be used to test the value of the ERRORLEVEL variable.

您将需要使用默认值从数据库中获取错误.没有它们,您看到的errorlevel是指已执行的sqlcmd实例(应成功执行,应为0),而不是数据库中运行的命令.

You will need to use the adecuated values to get a error from the database. Without them, the errorlevel you see is refered to the sqlcmd instance executed (should be 0 as it has been sucessfully executed), not the command running in the database.

此外,如前所述,ECHO is off.消息是由您

Also, as already commented, the ECHO is off. message is generated by your

echo %Message%

,变量未定义.该行被解析并转换为

with the variable undefined. The line is parsed and converted to

echo 

,因为%Message%没有值.而echo命令不带参数的行为是输出echo状态.

as %Message% has no value. And the behaviour of echo command without parameters is to output the echo state.

这篇关于批处理文件中的错误级别不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆