XACT_STATE (Transact-SQL)XACT_STATE (Transact-SQL)
適用対象:Applies to: SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Parallel Data WarehouseParallel Data Warehouse
Parallel Data WarehouseParallel Data Warehouse
SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Parallel Data WarehouseParallel Data Warehouse
Parallel Data WarehouseParallel Data Warehouse
現在実行されている要求のユーザー トランザクション状態を報告するスカラー関数です。Is a scalar function that reports the user transaction state of a current running request. XACT_STATE は、要求にアクティブなユーザー トランザクションがあるかどうか、およびそのトランザクションがコミット可能かどうかを示します。XACT_STATE indicates whether the request has an active user transaction, and whether the transaction is capable of being committed.
Transact-SQL 構文表記規則
Transact-SQL Syntax Conventions
構文Syntax
XACT_STATE()
注意
SQL Server 2014 以前の Transact-SQL 構文を確認するには、以前のバージョンのドキュメントを参照してください。To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
注意
この構文は、Azure Synapse Analytics のサーバーレス SQL プールでサポートされていません。This syntax is not supported by serverless SQL pool in Azure Synapse Analytics.
戻り値の型Return Type
smallintsmallint
注釈Remarks
XACT_STATE では次の値が返されます。XACT_STATE returns the following values.
戻り値Return value | 説明Meaning |
---|---|
11 | 現在の要求にアクティブなユーザー トランザクションが存在する。The current request has an active user transaction. 要求では、データ書き込み、トランザクションのコミットなど、任意の操作を実行できます。The request can perform any actions, including writing data and committing the transaction. |
00 | 現在の要求にアクティブなユーザー トランザクションが存在しない。There is no active user transaction for the current request. |
-1-1 | 現在の要求にアクティブなユーザー トランザクションが存在するが、コミットできないトランザクションとして分類されるエラーが発生した。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. 外側のバッチの実行が完了すると、コミット不可能なトランザクションは、データベース エンジンDatabase Engine によって自動的にロールバックされます。When the outermost batch finishes running, the データベース エンジンDatabase Engine will automatically roll back any active uncommittable transactions. トランザクションがコミット不可能な状態になったときにエラー メッセージが送信されなかった場合、バッチが完了すると、エラー メッセージがクライアント アプリケーションに送信されます。If no error message was sent when the transaction entered an uncommittable state, when the batch finishes, an error message will be sent to the client application. このメッセージは、コミット不可能なトランザクションが検出され、ロールバックされたことを示します。This message indicates that an uncommittable transaction was detected and rolled back. |
現在の要求にアクティブなユーザー トランザクションがあるかどうかを検出するには、XACT_STATE 関数と @@TRANCOUNT 関数の両方を使用できます。Both the XACT_STATE and @@TRANCOUNT functions can be used to detect whether the current request has an active user transaction. ただし、@@TRANCOUNT では、検出されたトランザクションがコミット不可能なトランザクションとして分類されているかどうかは判断できません。@@TRANCOUNT cannot be used to determine whether that transaction has been classified as an uncommittable transaction. また、XACT_STATE では、入れ子になったトランザクションが存在するかどうかは判断できません。XACT_STATE cannot be used to determine whether there are nested transactions.
例Examples
次の例では、XACT_STATE
構造の CATCH
ブロックで TRY...CATCH
を使用し、トランザクションをコミットまたはロールバックするかどうかを確認します。The following example uses XACT_STATE
in the CATCH
block of a TRY...CATCH
construct to determine whether to commit or roll back a transaction. ここでは SET XACT_ABORT
が ON
に設定されているため、制約違反エラーによってトランザクションはコミットできない状態になります。Because SET XACT_ABORT
is ON
, the constraint violation error causes the transaction to enter an uncommittable state.
USE AdventureWorks2012;
GO
-- SET XACT_ABORT ON will render the transaction uncommittable
-- when the constraint violation occurs.
SET XACT_ABORT ON;
BEGIN TRY
BEGIN TRANSACTION;
-- A FOREIGN KEY constraint exists on this table. This
-- statement will generate a constraint violation error.
DELETE FROM Production.Product
WHERE ProductID = 980;
-- If the delete operation succeeds, commit the transaction. The CATCH
-- block will not execute.
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
-- Test XACT_STATE for 0, 1, or -1.
-- If 1, the transaction is committable.
-- If -1, the transaction is uncommittable and should
-- be rolled back.
-- XACT_STATE = 0 means there is no transaction and
-- a commit or rollback operation would generate an error.
-- Test whether the transaction is uncommittable.
IF (XACT_STATE()) = -1
BEGIN
PRINT 'The transaction is in an uncommittable state.' +
' Rolling back transaction.'
ROLLBACK TRANSACTION;
END;
-- Test whether the transaction is active and valid.
IF (XACT_STATE()) = 1
BEGIN
PRINT 'The transaction is committable.' +
' Committing transaction.'
COMMIT TRANSACTION;
END;
END CATCH;
GO
参照See Also
@@TRANCOUNT (Transact-SQL) @@TRANCOUNT (Transact-SQL)
BEGIN TRANSACTION (Transact-SQL) BEGIN TRANSACTION (Transact-SQL)
COMMIT TRANSACTION (Transact-SQL) COMMIT TRANSACTION (Transact-SQL)
ROLLBACK TRANSACTION (Transact-SQL) ROLLBACK TRANSACTION (Transact-SQL)
SAVE TRANSACTION (Transact-SQL) SAVE TRANSACTION (Transact-SQL)
TRY...CATCH (Transact-SQL)TRY...CATCH (Transact-SQL)