gpt4 book ai didi

error-handling - 从DOS进行错误测试和控制

转载 作者:行者123 更新时间:2023-12-03 08:34:03 25 4
gpt4 key购买 nike

我正在Windows服务器企业系统SP2上运行DOS 6.0.6002。
SQL Server 2008 R2(10.50.4000)
我在DOS中有一个主控制程序。
我正在通过sqlcmd调用一个sql程序。
简化版本如下所示:

set sqlsvr=myServer
set logfile=logfile.txt
sqlcmd -S %sqlsvr% -d myDB -i import_some_stuff.sql > "%logfile%" 2>&1
echo error level = %ERRORLEVEL%

我需要这个程序非常强大。它必须每天针对大量文件和表运行。如果失败,我需要捕获它并通知sysadmin。现在,只需捕获它。

因此,为了测试这一点,我尝试了以下测试:

1)将文件重命名为不存在的文件。
结果:它返回并且错误级别为1(即捕获了错误!)。

2)在sql程序的前面输入一些语法垃圾。
结果:它在日志文件中打印错误消息,但它不返回错误(因此%ERRORLEVEL%中的返回值为零。这对我来说似乎令人难以置信。我缺少什么?

最佳答案

尝试对sqlcmd使用-b选项:

-b

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).

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



这是 documentation

关于error-handling - 从DOS进行错误测试和控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15574363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com