Skip to content

Commit ef8f586

Browse files
Merge pull request #34538 from MicrosoftDocs/main
Merged by Learn.Build PR Management system
2 parents 720ee6e + 170ad24 commit ef8f586

File tree

1 file changed

+52
-50
lines changed

1 file changed

+52
-50
lines changed

docs/t-sql/queries/with-common-table-expression-transact-sql.md

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: "Transact-SQL reference for how to use common table expressions (CT
44
author: VanMSFT
55
ms.author: vanto
66
ms.reviewer: randolphwest
7-
ms.date: 06/24/2025
7+
ms.date: 06/25/2025
88
ms.service: sql
99
ms.subservice: t-sql
1010
ms.topic: reference
@@ -32,7 +32,7 @@ monikerRange: ">=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >
3232

3333
[!INCLUDE [sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw-fabricsqldb](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw-fabricsqldb.md)]
3434

35-
Specifies a temporary named result set, known as a common table expression (CTE). This is derived from a simple query and defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or MERGE statement. This clause can also be used in a CREATE VIEW statement as part of its defining SELECT statement. A common table expression can include references to itself. This is referred to as a recursive common table expression.
35+
Specifies a temporary named result set, known as a common table expression (CTE). This is derived from a simple query and defined within the execution scope of a single `SELECT`, `INSERT`, `UPDATE`, `MERGE`, or `DELETE` statement. This clause can also be used in a `CREATE VIEW` statement as part of its defining `SELECT` statement. A common table expression can include references to itself. This is referred to as a recursive common table expression.
3636

3737
:::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)
3838

@@ -59,85 +59,87 @@ Specifies a column name in the common table expression. Duplicate names within a
5959

6060
#### *CTE_query_definition*
6161

62-
Specifies a SELECT statement whose result set populates the common table expression. The SELECT statement for *CTE_query_definition* must meet the same requirements as for creating a view, except a CTE can't define another CTE. For more information, see the Remarks section and [CREATE VIEW](../statements/create-view-transact-sql.md).
62+
Specifies a `SELECT` statement whose result set populates the common table expression. The `SELECT` statement for *CTE_query_definition* must meet the same requirements as for creating a view, except a CTE can't define another CTE. For more information, see the Remarks section and [CREATE VIEW](../statements/create-view-transact-sql.md).
6363

64-
If more than one *CTE_query_definition* is defined, the query definitions must be joined by one of these set operators: UNION ALL, UNION, EXCEPT, or INTERSECT.
64+
If more than one *CTE_query_definition* is defined, the query definitions must be joined by one of these set operators: `UNION ALL`, `UNION`, `EXCEPT`, or `INTERSECT`.
6565

66-
## Guidelines for creating and using common table expressions
66+
## Usage guidelines
6767

68-
The following guidelines apply to nonrecursive common table expressions. For guidelines that apply to recursive common table expressions, see [Guidelines for Defining and Using Recursive Common Table Expressions](#guidelines-for-defining-and-using-recursive-common-table-expressions) that follows.
68+
- [Guidelines for common table expressions](#guidelines-for-nonrecursive-common-table-expressions)
69+
- [Guidelines for recursive common table expressions](#guidelines-for-recursive-common-table-expressions)
6970

70-
- A CTE must be followed by a single `SELECT`, `INSERT`, `UPDATE`, or `DELETE` statement that references some or all the CTE columns. A CTE can also be specified in a `CREATE VIEW` statement as part of the defining `SELECT` statement of the view.
71+
### Guidelines for nonrecursive common table expressions
7172

72-
- Multiple CTE query definitions can be defined in a nonrecursive CTE. The definitions must be combined by one of these set operators: `UNION ALL`, `UNION`, `INTERSECT`, or `EXCEPT`.
73+
> [!NOTE]
74+
> The following guidelines apply to nonrecursive common table expressions. For guidelines that apply to recursive common table expressions, see [Guidelines for recursive common table expressions](#guidelines-for-recursive-common-table-expressions).
7375
74-
- A CTE can reference itself and previously defined CTEs in the same `WITH` clause. Forward referencing isn't allowed.
76+
A CTE must be followed by a single `SELECT`, `INSERT`, `UPDATE`, `MERGE`, or `DELETE` statement that references some or all the CTE columns. A CTE can also be specified in a `CREATE VIEW` statement as part of the defining `SELECT` statement of the view.
7577

76-
- Specifying more than one WITH clause in a CTE isn't allowed. For example, if a *CTE_query_definition* contains a subquery, that subquery can't contain a nested WITH clause that defines another CTE.
78+
Multiple CTE query definitions can be defined in a nonrecursive CTE. The definitions must be combined by one of these set operators: `UNION ALL`, `UNION`, `INTERSECT`, or `EXCEPT`.
7779

78-
- For more information on nested CTEs in Microsoft Fabric, see [Nested Common Table Expression (CTE) in Fabric data warehousing (Transact-SQL)](nested-common-table-expression.md?view=fabric&preserve-view=true).
80+
A CTE can reference itself and previously defined CTEs in the same `WITH` clause. Forward referencing isn't allowed.
7981

80-
- Query results from common table expressions aren't materialized. Each outer reference to the named result set requires the defined query to be re-executed. For queries that require multiple references to the named result set, consider using a [temporary object](../statements/create-table-transact-sql.md#temporary-tables) instead.
82+
Specifying more than one `WITH` clause in a CTE isn't allowed. For example, if a *CTE_query_definition* contains a subquery, that subquery can't contain a nested `WITH` clause that defines another CTE.
8183

82-
- You can't execute a stored procedure in a common table expression.
84+
For more information on nested CTEs in Microsoft Fabric, see [Nested Common Table Expression (CTE) in Fabric data warehousing (Transact-SQL)](nested-common-table-expression.md?view=fabric&preserve-view=true).
8385

84-
- The following clauses can't be used in the *CTE_query_definition*:
86+
Query results from common table expressions aren't materialized. Each outer reference to the named result set requires the defined query to be re-executed. For queries that require multiple references to the named result set, consider using a [temporary object](../statements/create-table-transact-sql.md#temporary-tables) instead.
8587

86-
- `ORDER BY` (except when a `TOP` or `OFFSET/FETCH` clause is specified)
88+
You can't execute a stored procedure in a common table expression.
8789

88-
- `INTO`
90+
The following clauses can't be used in the *CTE_query_definition*:
8991

90-
- `OPTION` clause with query hints
92+
- `ORDER BY` (except when a `TOP` or `OFFSET/FETCH` clause is specified)
93+
- `INTO`
94+
- `OPTION` clause with query hints <sup>1</sup>
95+
- `FOR BROWSE`
9196

92-
- `FOR BROWSE`
97+
<sup>1</sup> The `OPTION` clause can't be used inside a CTE definition. It can only be used in the outermost `SELECT` statement.
9398

94-
- When a CTE is used in a statement that is part of a batch, the statement before it must be followed by a semicolon.
95-
96-
- A query referencing a CTE can be used to define a cursor.
97-
98-
- Tables on remote servers can be referenced in the CTE.
99-
100-
- When executing a CTE, any hints that reference a CTE can conflict with other hints that are discovered when the CTE accesses its underlying tables, in the same manner as hints that reference views in queries. When this occurs, the query returns an error.
101-
102-
## Guidelines for defining and using recursive common table expressions
103-
104-
The following guidelines apply to defining a recursive common table expression:
105-
106-
- The recursive CTE definition must contain at least two CTE query definitions, an anchor member and a recursive member. Multiple anchor members and recursive members can be defined; however, all anchor member query definitions must be put before the first recursive member definition. All CTE query definitions are anchor members unless they reference the CTE itself.
99+
When a CTE is used in a statement that is part of a batch, the statement before it must be followed by a semicolon.
107100

108-
- Anchor members must be combined by one of these set operators: UNION ALL, UNION, INTERSECT, or EXCEPT. UNION ALL is the only set operator allowed between the last anchor member and first recursive member, and when combining multiple recursive members.
101+
A query referencing a CTE can be used to define a cursor.
109102

110-
- The number of columns in the anchor and recursive members must be the same.
103+
Tables on remote servers can be referenced in the CTE.
111104

112-
- The data type of a column in the recursive member must be the same as the data type of the corresponding column in the anchor member.
105+
When executing a CTE, any hints that reference a CTE can conflict with other hints that are discovered when the CTE accesses its underlying tables, in the same manner as hints that reference views in queries. When this occurs, the query returns an error.
113106

114-
- The FROM clause of a recursive member must refer only one time to the CTE *expression_name*.
107+
<a id="guidelines-for-defining-and-using-recursive-common-table-expressions"></a>
115108

116-
- The following items aren't allowed in the *CTE_query_definition* of a recursive member:
109+
### Guidelines for recursive common table expressions
117110

118-
- `SELECT DISTINCT`
111+
> [!NOTE]
112+
> The following guidelines apply to defining a recursive common table expression. For guidelines that apply to nonrecursive CTEs, see [Guidelines for nonrecursive common table expressions](#guidelines-for-nonrecursive-common-table-expressions).
119113
120-
- `GROUP BY`
114+
The recursive CTE definition must contain at least two CTE query definitions, an anchor member and a recursive member. Multiple anchor members and recursive members can be defined; however, all anchor member query definitions must be put before the first recursive member definition. All CTE query definitions are anchor members unless they reference the CTE itself.
121115

122-
- `PIVOT` (When the database compatibility level is 110 or higher. See [Breaking changes to Database Engine features in SQL Server 2016](../../database-engine/breaking-changes-to-database-engine-features-in-sql-server-2016.md).)
116+
Anchor members must be combined by one of these set operators: `UNION ALL`, `UNION`, `INTERSECT`, or `EXCEPT`. `UNION ALL` is the only set operator allowed between the last anchor member and first recursive member, and when combining multiple recursive members.
123117

124-
- `HAVING`
118+
The number of columns in the anchor and recursive members must be the same.
125119

126-
- Scalar aggregation
120+
The data type of a column in the recursive member must be the same as the data type of the corresponding column in the anchor member.
127121

128-
- `TOP`
122+
The FROM clause of a recursive member must refer only one time to the CTE *expression_name*.
129123

130-
- `LEFT`, `RIGHT`, `OUTER JOIN` (`INNER JOIN` is allowed)
124+
The following items aren't allowed in the *CTE_query_definition* of a recursive member:
131125

132-
- Subqueries
126+
- `SELECT DISTINCT`
127+
- `GROUP BY`
128+
- `PIVOT` <sup>1</sup>
129+
- `HAVING`
130+
- Scalar aggregation
131+
- `TOP`
132+
- `LEFT`, `RIGHT`, `OUTER JOIN` (`INNER JOIN` is allowed)
133+
- Subqueries
134+
- A hint applied to a recursive reference to a CTE inside a *CTE_query_definition*.
133135

134-
- A hint applied to a recursive reference to a CTE inside a *CTE_query_definition*.
136+
<sup>1</sup> When the database compatibility level is 110 or higher. See [Breaking changes to Database Engine features in SQL Server 2016](../../database-engine/breaking-changes-to-database-engine-features-in-sql-server-2016.md).
135137

136138
The following guidelines apply to using a recursive common table expression:
137139

138140
- All columns returned by the recursive CTE are nullable regardless of the nullability of the columns returned by the participating `SELECT` statements.
139141

140-
- An incorrectly composed recursive CTE can cause an infinite loop. For example, if the recursive member query definition returns the same values for both the parent and child columns, an infinite loop is created. To prevent an infinite loop, you can limit the number of recursion levels allowed for a particular statement by using the `MAXRECURSION` hint and a value between 0 and 32,767 in the OPTION clause of the `INSERT`, `UPDATE`, `DELETE`, or `SELECT` statement. This lets you control the execution of the statement until you resolve the code problem that is creating the loop. The server-wide default is 100. When 0 is specified, no limit is applied. Only one `MAXRECURSION` value can be specified per statement. For more information, see [Query hints](hints-transact-sql-query.md).
142+
- An incorrectly composed recursive CTE can cause an infinite loop. For example, if the recursive member query definition returns the same values for both the parent and child columns, an infinite loop is created. To prevent an infinite loop, you can limit the number of recursion levels allowed for a particular statement by using the `MAXRECURSION` hint and a value between `0` and `32767` in the `OPTION` clause of the `INSERT`, `UPDATE`, `DELETE`, or `SELECT` statement. This lets you control the execution of the statement until you resolve the code problem that is creating the loop. The server-wide default is 100. When 0 is specified, no limit is applied. Only one `MAXRECURSION` value can be specified per statement. For more information, see [Query hints](hints-transact-sql-query.md).
141143

142144
- A view that contains a recursive common table expression can't be used to update data.
143145

@@ -173,7 +175,7 @@ The current implementation of CTEs in [!INCLUDE [ssazuresynapse-md](../../includ
173175

174176
- Specifying more than one `WITH` clause in a CTE isn't allowed. For example, if a CTE query definition contains a subquery, that subquery can't contain a nested `WITH` clause that defines another CTE.
175177

176-
- An `ORDER BY` clause can't be used in the CTE_query_definition, except when a `TOP` clause is specified.
178+
- An `ORDER BY` clause can't be used in the *CTE_query_definition*, except when a `TOP` clause is specified.
177179

178180
- When a CTE is used in a statement that is part of a batch, the statement before it must be followed by a semicolon.
179181

@@ -223,7 +225,7 @@ FROM Sales_CTE;
223225

224226
### C. Use multiple CTE definitions in a single query
225227

226-
The following example shows how to define more than one CTE in a single query. A comma is used to separate the CTE query definitions. The FORMAT function, used to display the monetary amounts in a currency format, is available in SQL Server 2012 and later versions.
228+
The following example shows how to define more than one CTE in a single query. A comma is used to separate the CTE query definitions. The `FORMAT` function, used to display the monetary amounts in a currency format, was introduced in [!INCLUDE [sssql11-md](../../includes/sssql11-md.md)].
227229

228230
```sql
229231
WITH Sales_CTE (SalesPersonID, TotalSales, SalesYear)
@@ -394,7 +396,7 @@ FROM cte
394396
OPTION (MAXRECURSION 2);
395397
```
396398

397-
After the coding error is corrected, MAXRECURSION is no longer required. The following example shows the corrected code.
399+
After the coding error is corrected, `MAXRECURSION` is no longer required. The following example shows the corrected code.
398400

399401
```sql
400402
WITH cte (EmployeeID, ManagerID, Title) AS
@@ -455,7 +457,7 @@ ORDER BY ComponentLevel, AssemblyID, ComponentID;
455457

456458
### F. Use a recursive CTE in an UPDATE statement
457459

458-
The following example updates the `PerAssemblyQty` value for all parts that are used to build the product 'Road-550-W Yellow, 44' `(ProductAssemblyID``800`). The common table expression returns a hierarchical list of parts that are used to build `ProductAssemblyID 800` and the components that are used to create those parts, and so on. Only the rows returned by the common table expression are modified.
460+
The following example updates the `PerAssemblyQty` value for all parts that are used to build the product `'Road-550-W Yellow, 44' (ProductAssemblyID 800)`. The common table expression returns a hierarchical list of parts that are used to build `ProductAssemblyID 800` and the components that are used to create those parts, and so on. Only the rows returned by the common table expression are modified.
459461

460462
```sql
461463
USE AdventureWorks2022;

0 commit comments

Comments
 (0)