T-SQL 中的 TRY 和 RAISERROR

发布于 2024-07-26 15:04:12 字数 453 浏览 1 评论 0原文

有一个小问题,想知道我是否正确使用了这些。

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵强ㄟ 2024-08-02 15:04:12

这是因为 RAISERROR 的严重性不够高,需要介于 11 和 19 之间,如 此处

例如

RAISERROR ('Errors found, please fix these errors and retry', 16, 2) WITH SETERROR

It's because the severity of the RAISERROR is not high enough, needs to be between 11 and 19, as described here

e.g.

RAISERROR ('Errors found, please fix these errors and retry', 16, 2) WITH SETERROR
痞味浪人 2024-08-02 15:04:12

我认为你需要提出一个严重级别高于 10 的错误才能被捕获,例如

RAISERROR ('Errors found', 11, 2) WITH SETERROR

I think you need to raise an error with a severity level higher than 10 for it to be caught, e.g.

RAISERROR ('Errors found', 11, 2) WITH SETERROR
一杆小烟枪 2024-08-02 15:04:12

来自 MSDN


严重性

是与此消息关联的用户定义的严重性级别。 任何用户都可以使用从 0 到 18 的严重级别。 19 到 25 之间的严重级别仅由 sysadmin 固定服务器角色的成员使用。 对于从 19 到 25 的严重级别,需要使用“WITH LOG”选项。

警告 20 到 25 的严重级别被认为是致命的。 如果遇到致命严重级别,客户端连接将在收到消息后终止,并将错误记录在错误日志和应用程序日志中。


请尝试以下操作:

RAISERROR ('Errors found, please fix these errors and retry', 1, 2) WITH SETERROR
RETURN

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:

RAISERROR ('Errors found, please fix these errors and retry', 1, 2) WITH SETERROR
RETURN
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文