XACT_ABORT ON 在 SQL Server 2012 上没有按预期工作?

Ale*_*exC 5 sql-server error-handling sql-server-2012

看起来 XACT_ABORT ON 没有按预期工作。这是一个表和一个插入其中的过程:

CREATE TABLE rain
    (
      rain_time DATETIME ,
      location VARCHAR(100)
    );
GO

CREATE PROCEDURE insert_rain
    @rain_time DATETIME ,
    @location VARCHAR(100)
AS
    BEGIN;
        SET XACT_ABORT ON;
        BEGIN TRANSACTION;
        PRINT 'before insert';
        INSERT  INTO rain
                ( rain_time, location )
        VALUES  ( @rain_time, @location );
        PRINT 'after insert';
        COMMIT;
    END;
GO
Run Code Online (Sandbox Code Playgroud)

我创建了一个触发器来模拟运行时错误:

CREATE TRIGGER rain_no_insert
ON rain
FOR INSERT 
AS
RAISERROR('Cannot insert', 16, 1);
GO
Run Code Online (Sandbox Code Playgroud)

当我调用插入过程时,我确实收到了错误,但执行并没有停止,因为最后一个 PRINT 打印了它的“插入后”消息:

EXEC insert_rain
    @rain_time = '2015-03-30 12:34:56',
    @location = 'Wautoma, WI';

before insert
Msg 50000, Level 16, State 1, Procedure rain_no_insert, Line 5
Cannot insert

(1 row(s) affected)
after insert
Run Code Online (Sandbox Code Playgroud)

事务也提交:

SELECT * FROM dbo.rain;

rain_time               location
----------------------- ---------------------------------
2015-03-30 12:34:56.000 Wautoma, WI
Run Code Online (Sandbox Code Playgroud)

为什么我的 XACT_ABORT 没有中止?

Kri*_*yer 11

检查 Microsoft 文档,特别是最上面的一行:

THROW 语句遵循 SET XACT_ABORT。RAISERROR 没有。新应用程序应使用 THROW 而不是 RAISERROR。

我相信这是你的问题,就在那里。

https://msdn.microsoft.com/en-us/library/ms188792.aspx