发生错误导致事务被归类为不可提交的事务

An error has occurred that has caused the transaction to be classified as an uncommittable transaction

XACT_STATE() 可以有 1、0、-1 个值。当它为 1 时,可以执行任何操作,但是当它为 -1 时,只能执行读取或回滚。在什么场景下事务被归类为不可提交事务。

Here 是 msdn 所说的。

The current request has an active user transaction, but an error has occurred that has caused the transaction to be classified as an uncommittable transaction. The request cannot commit the transaction or roll back to a savepoint; it can only request a full rollback of the transaction. The request cannot perform any write operations until it rolls back the transaction. The request can only perform read operations until it rolls back the transaction. After the transaction has been rolled back, the request can perform both read and write operations and can begin a new transaction.

摘自blog

当您的交易达到 16 级错误时,交易可能会进入注定状态 (XACT_STATE() =-1),但并不总是如上文所述!!

以下是博客中给出的一些示例:

--datatype conversion errors:

   DECLARE @GUID UNIQUEIDENTIFIER 
   SELECT @GUID = CONVERT(UNIQUEIDENTIFIER, 'ABC')  

---divide by zero errors:
   SELECT * FROM my_books WHERE Isbn/0 = 2

--dropping column which doesnt exists      
 ALTER TABLE my_books DROP COLUMN author;    

作者也在下面解释了其他可能导致交易失败的错误级别..

可能会出现更多错误,使交易进入注定失败的状态! 用户可以参考此博客作为参考,以确定 severity\level 16 引发的错误是否会回滚事务。如果您愿意,还可以测试其他严重程度或级别的错误

还有一个 link 这将有助于模拟每个错误级别..