Skip to content

Commit bd28cec

Browse files
Refresh transaction DDL (#36094)
1 parent fb7baf9 commit bd28cec

9 files changed

Lines changed: 469 additions & 403 deletions

docs/relational-databases/accelerated-database-recovery-concepts.md

Lines changed: 29 additions & 27 deletions
Large diffs are not rendered by default.

docs/relational-databases/sql-server-transaction-locking-and-row-versioning-guide.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ title: "Transaction locking and row versioning guide"
33
description: "Transaction locking and row versioning guide"
44
author: MikeRayMSFT
55
ms.author: mikeray
6-
ms.reviewer: randolphwest, wiassaf
7-
ms.date: 06/20/2025
6+
ms.reviewer: randolphwest, wiassaf, dfurman
7+
ms.date: 12/16/2025
88
ms.service: sql
99
ms.subservice: performance
10-
ms.topic: conceptual
10+
ms.topic: concept-article
1111
ms.custom:
1212
- ignite-2025
1313
helpviewer_keywords:
@@ -1766,11 +1766,11 @@ The granularity of locking used on an index can be set using the `CREATE INDEX`
17661766

17671767
## <a id="Advanced"></a> Advanced transaction information
17681768

1769-
### <a id="nesting-transactions"></a> Nest transactions
1769+
### <a id="nesting-transactions"></a> Outer and inner transactions
17701770

1771-
Explicit transactions can be nested. This is primarily intended to support transactions in stored procedures that can be called either from a process already in a transaction or from processes that have no active transaction.
1771+
An explicit inner transaction can be started within an explicit outer transaction. This is primarily intended to support transactions in stored procedures that can be called either from a process already in a transaction or from processes that have no active transaction.
17721772

1773-
The following example shows the use of nested transactions. If `TransProc` is called when a transaction is active, the outcome of the nested transaction in `TransProc` is controlled by the outer transaction, and its `INSERT` statements are committed or rolled back based on the commit or roll back of the outer transaction. If `TransProc` is executed by a process that doesn't have an outstanding transaction, the `COMMIT TRANSACTION` at the end of the procedure commits the `INSERT` statements.
1773+
The following example shows the use of outer and inner transactions. If `TransProc` is called when a transaction is active, the outcome of the inner transaction in `TransProc` is controlled by the outer transaction, and its `INSERT` statements are committed or rolled back based on the commit or roll back of the outer transaction. If `TransProc` is executed by a process that doesn't have an outstanding transaction, the `COMMIT TRANSACTION` at the end of the procedure commits the `INSERT` statements.
17741774

17751775
```sql
17761776
SET QUOTED_IDENTIFIER OFF;
@@ -1805,7 +1805,7 @@ EXEC TransProc 1, 'aaa';
18051805
GO
18061806

18071807
/* Roll back the outer transaction, this will
1808-
roll back TransProc's nested transaction. */
1808+
roll back TransProc's inner transaction. */
18091809
ROLLBACK TRANSACTION OutOfProc;
18101810
GO
18111811

@@ -1824,13 +1824,16 @@ FROM TestTrans;
18241824
GO
18251825
```
18261826

1827-
Committing inner transactions is ignored by the [!INCLUDE [Database Engine](../includes/ssde-md.md)] when an outer transaction is active. The transaction is either committed or rolled back based on the commit or roll back at the end of the outermost transaction. If the outer transaction is committed, the inner nested transactions are also committed. If the outer transaction is rolled back, then all inner transactions are also rolled back, regardless of whether or not the inner transactions were individually committed.
1827+
Committing inner transactions is ignored by the [!INCLUDE [Database Engine](../includes/ssde-md.md)] when an outer transaction is active. The transaction is either committed or rolled back based on the commit or roll back at the end of the outermost transaction. If the outer transaction is committed, the inner transactions are also committed. If the outer transaction is rolled back, then all inner transactions are also rolled back, regardless of whether or not the inner transactions were individually committed.
18281828

1829-
Each call to `COMMIT TRANSACTION` or `COMMIT WORK` applies to the last executed `BEGIN TRANSACTION`. If the `BEGIN TRANSACTION` statements are nested, then a `COMMIT` statement applies only to the last nested transaction, which is the innermost transaction. Even if a `COMMIT TRANSACTION transaction_name` statement within a nested transaction refers to the transaction name of the outer transaction, the commit applies only to the innermost transaction.
1829+
Each call to `COMMIT TRANSACTION` or `COMMIT WORK` applies to the last executed `BEGIN TRANSACTION`. If there are multiple `BEGIN TRANSACTION` statements, then a `COMMIT` statement applies only to the last statement, in other words to the innermost transaction. Even if a `COMMIT TRANSACTION transaction_name` statement within an inner transaction refers to the transaction name of the outer transaction, the commit applies only to the innermost transaction.
18301830

1831-
It isn't allowed for the `transaction_name` parameter of a `ROLLBACK TRANSACTION` statement to refer to the inner transaction in a set of named nested transactions. `transaction_name` can refer only to the transaction name of the outermost transaction. If a `ROLLBACK TRANSACTION transaction_name` statement using the name of the outer transaction is executed at any level of a set of nested transactions, all of the nested transactions are rolled back. If a `ROLLBACK WORK` or `ROLLBACK TRANSACTION` statement without a `transaction_name` parameter is executed at any level of a set of nested transaction, it rolls back all of the nested transactions, including the outermost transaction.
1831+
It isn't allowed for the `transaction_name` parameter of a `ROLLBACK TRANSACTION` statement to refer to the inner transaction in a set of named transactions. `transaction_name` can refer only to the transaction name of the outermost transaction.
18321832

1833-
The `@@TRANCOUNT` function records the current transaction nesting level. Each `BEGIN TRANSACTION` statement increments `@@TRANCOUNT` by one. Each `COMMIT TRANSACTION` or `COMMIT WORK` statement decrements `@@TRANCOUNT` by one. A `ROLLBACK WORK` or a `ROLLBACK TRANSACTION` statement that doesn't have a transaction name rolls back all nested transactions and decrements `@@TRANCOUNT` to 0. A `ROLLBACK TRANSACTION` that uses the transaction name of the outermost transaction in a set of nested transactions rolls back all of the nested transactions and decrements `@@TRANCOUNT` to 0. To determine if you are already in a transaction, `SELECT @@TRANCOUNT` to see if it is 1 or more. If `@@TRANCOUNT` is 0, you are not in a transaction.
1833+
The `@@TRANCOUNT` function records the current transaction nesting level. Each `BEGIN TRANSACTION` statement increments `@@TRANCOUNT` by one. Each `COMMIT TRANSACTION` or `COMMIT WORK` statement decrements `@@TRANCOUNT` by one. A `ROLLBACK WORK` or a `ROLLBACK TRANSACTION` statement that doesn't have a transaction name rolls back the outer and all inner transactions and decrements `@@TRANCOUNT` to 0. Similarly, a `ROLLBACK TRANSACTION` that uses the transaction name of the outermost transaction rolls back the outer and all inner transactions and decrements `@@TRANCOUNT` to 0. To determine if you are already in a transaction, `SELECT @@TRANCOUNT` to see if it is 1 or more. If `@@TRANCOUNT` is 0, you are not in a transaction.
1834+
1835+
> [!NOTE]
1836+
> The [!INCLUDE [ssde-md](../includes/ssde-md.md)] doesn't support independently manageable nested transactions. A commit of an inner transaction decrements `@@TRANCOUNT` but has no other effects. A rollback of an inner transaction always rolls back the outer transaction, unless a [savepoint](../t-sql/language-elements/save-transaction-transact-sql.md) exists and is specified in the `ROLLBACK` statement.
18341837
18351838
### <a id="using-bound-sessions"></a> Use bound sessions
18361839

docs/t-sql/functions/xact-state-transact-sql.md

Lines changed: 78 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ title: "XACT_STATE (Transact-SQL)"
33
description: "XACT_STATE (Transact-SQL)"
44
author: MikeRayMSFT
55
ms.author: mikeray
6-
ms.date: "03/16/2017"
6+
ms.reviewer: dfurman, randolphwest
7+
ms.date: 12/17/2025
78
ms.service: sql
89
ms.subservice: t-sql
910
ms.topic: reference
@@ -23,92 +24,86 @@ dev_langs:
2324
- "TSQL"
2425
monikerRange: ">=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric-sqldb"
2526
---
27+
2628
# XACT_STATE (Transact-SQL)
29+
2730
[!INCLUDE [sql-asdb-asdbmi-asa-pdw-fabricsqldb](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw-fabricsqldb.md)]
2831

29-
Is a scalar function that reports the user transaction state of a current running request. XACT_STATE indicates whether the request has an active user transaction, and whether the transaction is capable of being committed.
30-
31-
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
32-
33-
## Syntax
34-
35-
```syntaxsql
36-
XACT_STATE()
37-
```
32+
Is a scalar function that reports the user transaction state of the current session. `XACT_STATE` indicates whether the session has an active user transaction, and whether the transaction is capable of being committed.
33+
34+
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
35+
36+
## Syntax
37+
38+
```syntaxsql
39+
XACT_STATE()
40+
```
3841

3942
> [!NOTE]
40-
> [!INCLUDE[synapse-analytics-od-unsupported-syntax](../../includes/synapse-analytics-od-unsupported-syntax.md)]
41-
42-
## Return Type
43-
**smallint**
44-
45-
## Remarks
46-
XACT_STATE returns the following values.
47-
48-
|Return value|Meaning|
49-
|------------------|-------------|
50-
|1|The current request has an active user transaction. The request can perform any actions, including writing data and committing the transaction.|
51-
|0|There is no active user transaction for the current request.|
52-
|-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.<br /><br /> When the outermost batch finishes running, the [!INCLUDE[ssDE](../../includes/ssde-md.md)] 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.|
53-
54-
Both the XACT_STATE and @@TRANCOUNT functions can be used to detect whether the current request has an active user transaction. @@TRANCOUNT cannot be used to determine whether that transaction has been classified as an uncommittable transaction. XACT_STATE cannot be used to determine whether there are nested transactions.
55-
56-
## Examples
57-
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. Because `SET XACT_ABORT` is `ON`, the constraint violation error causes the transaction to enter an uncommittable state.
58-
59-
```sql
60-
USE AdventureWorks2022;
61-
GO
62-
63-
-- SET XACT_ABORT ON will render the transaction uncommittable
64-
-- when the constraint violation occurs.
65-
SET XACT_ABORT ON;
66-
67-
BEGIN TRY
68-
BEGIN TRANSACTION;
69-
-- A FOREIGN KEY constraint exists on this table. This
70-
-- statement will generate a constraint violation error.
71-
DELETE FROM Production.Product
72-
WHERE ProductID = 980;
73-
74-
-- If the delete operation succeeds, commit the transaction. The CATCH
75-
-- block will not execute.
76-
COMMIT TRANSACTION;
77-
END TRY
78-
BEGIN CATCH
79-
-- Test XACT_STATE for 0, 1, or -1.
80-
-- If 1, the transaction is committable.
81-
-- If -1, the transaction is uncommittable and should
82-
-- be rolled back.
83-
-- XACT_STATE = 0 means there is no transaction and
84-
-- a commit or rollback operation would generate an error.
85-
86-
-- Test whether the transaction is uncommittable.
87-
IF (XACT_STATE()) = -1
88-
BEGIN
89-
PRINT 'The transaction is in an uncommittable state.' +
90-
' Rolling back transaction.'
91-
ROLLBACK TRANSACTION;
92-
END;
93-
94-
-- Test whether the transaction is active and valid.
95-
IF (XACT_STATE()) = 1
43+
> [!INCLUDE [synapse-analytics-od-unsupported-syntax](../../includes/synapse-analytics-od-unsupported-syntax.md)]
44+
45+
## Return types
46+
47+
**smallint**
48+
49+
## Remarks
50+
51+
`XACT_STATE` returns the following values.
52+
53+
| Return value | Description |
54+
| --- | --- |
55+
| 1 | The current session has an active user transaction. The session can perform any actions, including writing data and committing the transaction. |
56+
| 0 | There's no active user transaction for the current session. |
57+
| -1 | The current session has an active user transaction, but an error occurred that caused the transaction to be classified as an uncommittable transaction. The session can't commit the transaction or roll back to a savepoint; it can only request a full rollback of the transaction. The session can't perform any write operations until it rolls back the transaction. The session can only perform read operations until it rolls back the transaction. After the transaction is rolled back, the session can perform both read and write operations and can begin a new transaction.<br /><br />When the outermost batch finishes running, the [!INCLUDE [ssDE](../../includes/ssde-md.md)] automatically rolls 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 is sent to the client application. This message indicates that an uncommittable transaction was detected and rolled back. |
58+
59+
Both the `XACT_STATE` and `@@TRANCOUNT` functions can be used to detect whether the current session has an active user transaction. `@@TRANCOUNT` can't be used to determine whether that transaction is classified as an uncommittable transaction. `XACT_STATE` can't be used to determine whether there are inner transactions.
60+
61+
## Examples
62+
63+
[!INCLUDE [article-uses-adventureworks](../../includes/article-uses-adventureworks.md)]
64+
65+
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. Because `SET XACT_ABORT` is `ON`, the constraint violation error causes the transaction to enter an uncommittable state.
66+
67+
```sql
68+
-- SET XACT_ABORT ON renders the transaction uncommittable
69+
-- when the constraint violation occurs.
70+
SET XACT_ABORT ON;
71+
72+
BEGIN TRY
73+
BEGIN TRANSACTION;
74+
-- A FOREIGN KEY constraint exists on this table. This
75+
-- statement generates a constraint violation error.
76+
DELETE FROM Production.Product
77+
WHERE ProductID = 980;
78+
79+
-- If the delete operation succeeds, commit the transaction. The CATCH
80+
-- block does not execute.
81+
COMMIT TRANSACTION;
82+
END TRY
83+
BEGIN CATCH
84+
-- Test whether the transaction is uncommittable.
85+
IF XACT_STATE() = -1
9686
BEGIN
97-
PRINT 'The transaction is committable.' +
98-
' Committing transaction.'
99-
COMMIT TRANSACTION;
100-
END;
101-
END CATCH;
102-
GO
103-
```
104-
105-
## See Also
106-
[@@TRANCOUNT &#40;Transact-SQL&#41;](../../t-sql/functions/trancount-transact-sql.md)
107-
[BEGIN TRANSACTION &#40;Transact-SQL&#41;](../../t-sql/language-elements/begin-transaction-transact-sql.md)
108-
[COMMIT TRANSACTION &#40;Transact-SQL&#41;](../../t-sql/language-elements/commit-transaction-transact-sql.md)
109-
[ROLLBACK TRANSACTION &#40;Transact-SQL&#41;](../../t-sql/language-elements/rollback-transaction-transact-sql.md)
110-
[SAVE TRANSACTION &#40;Transact-SQL&#41;](../../t-sql/language-elements/save-transaction-transact-sql.md)
111-
[TRY...CATCH &#40;Transact-SQL&#41;](../../t-sql/language-elements/try-catch-transact-sql.md)
112-
113-
87+
PRINT 'The transaction is in an uncommittable state.' +
88+
' Rolling back transaction.'
89+
ROLLBACK TRANSACTION;
90+
END;
91+
92+
-- Test whether the transaction is active and valid.
93+
IF XACT_STATE() = 1
94+
BEGIN
95+
PRINT 'The transaction is committable.' +
96+
' Committing transaction.'
97+
COMMIT TRANSACTION;
98+
END;
99+
END CATCH;
100+
```
101+
102+
## Related context
114103

104+
- [@@TRANCOUNT (Transact-SQL)](trancount-transact-sql.md)
105+
- [BEGIN TRANSACTION (Transact-SQL)](../language-elements/begin-transaction-transact-sql.md)
106+
- [COMMIT TRANSACTION (Transact-SQL)](../language-elements/commit-transaction-transact-sql.md)
107+
- [ROLLBACK TRANSACTION (Transact-SQL)](../language-elements/rollback-transaction-transact-sql.md)
108+
- [SAVE TRANSACTION (Transact-SQL)](../language-elements/save-transaction-transact-sql.md)
109+
- [TRY...CATCH (Transact-SQL)](../language-elements/try-catch-transact-sql.md)

0 commit comments

Comments
 (0)