【问题标题】:Are these code snippets equivalent ('set xact_abort on' vs 'try catch rollback')?这些代码片段是否等效('set xact_abort on' vs 'try catch rollback')?
【发布时间】:2014-01-09 20:07:53
【问题描述】:

我曾经在 SQL Server 的存储过程中使用此代码 sn-p:

create procedure proc_name
    --declare variables
as
    set nocount on
    begin transaction
    begin try
        --do something
        commit transaction
    end try begin catch
        rollback transaction
        ;throw
    end catch
go

但是今天我知道了'set xact_abort on' 语句。 下面的代码是否等同于前面的代码?它们之间有什么区别吗?

create procedure proc_name
    --declare variables
as
    set nocount on
    set xact_abort on
    begin transaction
    --do something
    commit transaction
go

【问题讨论】:

    标签: sql-server xact-abort


    【解决方案1】:

    引用MS docs

    TRY...CATCH 构造会捕获所有严重程度高于 10 且不会关闭数据库连接的执行错误。

    因此,try catch 不会捕获所有可能的错误。除了 try catch 之外,您还可以使用 xact_abort on。

    try/catch 为您提供更大的灵活性,也就是说,您不仅限于在不满意时回滚。

    【讨论】:

    • «try catch 无法捕获所有可能的错误»。并且 xact_abort 在遇到任何错误时回滚 tran,包括严重级别
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 2016-02-14
    • 2015-03-22
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 2012-10-06
    相关资源
    最近更新 更多