T-SQL 中的 TRY 和 RAISERROR
有一个小问题,想知道我是否正确使用了这些。
在我的 SQL 脚本中有
BEGIN TRY
// check some information and if there are certains errors
RAISERROR ('Errors found, please fix these errors and retry', 1, 2) WITH SETERROR
// Complete normal process if no errors encountered above
PRINT 'IMPORT SUCCEEDED'
END TRY
BEGIN CATCH
PRINT 'IMPORT ABORTED. ERRORS ENCOUNTERED'
END CATCH
但是,这遇到了错误,然后继续执行脚本的其余部分。 我缺少什么? 谢谢!
Having a small issue and wondering if I'm using these correctly.
In my SQL script is have
BEGIN TRY
// check some information and if there are certains errors
RAISERROR ('Errors found, please fix these errors and retry', 1, 2) WITH SETERROR
// Complete normal process if no errors encountered above
PRINT 'IMPORT SUCCEEDED'
END TRY
BEGIN CATCH
PRINT 'IMPORT ABORTED. ERRORS ENCOUNTERED'
END CATCH
However, this is encountering an error and then continuing with the rest of the script. What am I missing? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为 RAISERROR 的严重性不够高,需要介于 11 和 19 之间,如 此处
例如
It's because the severity of the RAISERROR is not high enough, needs to be between 11 and 19, as described here
e.g.
我认为你需要提出一个严重级别高于 10 的错误才能被捕获,例如
I think you need to raise an error with a severity level higher than 10 for it to be caught, e.g.
来自 MSDN
严重性
是与此消息关联的用户定义的严重性级别。 任何用户都可以使用从 0 到 18 的严重级别。 19 到 25 之间的严重级别仅由 sysadmin 固定服务器角色的成员使用。 对于从 19 到 25 的严重级别,需要使用“WITH LOG”选项。
警告 20 到 25 的严重级别被认为是致命的。 如果遇到致命严重级别,客户端连接将在收到消息后终止,并将错误记录在错误日志和应用程序日志中。
请尝试以下操作:
From MSDN
severity
Is the user-defined severity level associated with this message. Severity levels from 0 through 18 can be used by any user. Severity levels from 19 through 25 are used only by members of the sysadmin fixed server role. For severity levels from 19 through 25, the WITH LOG option is required.
Caution Severity levels from 20 through 25 are considered fatal. If a fatal severity level is encountered, the client connection is terminated after receiving the message, and the error is logged in the error log and the application log.
Try this instead: