You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
@@ -59,85 +59,87 @@ Specifies a column name in the common table expression. Duplicate names within a
59
59
60
60
#### *CTE_query_definition*
61
61
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).
63
63
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`.
65
65
66
-
## Guidelines for creating and using common table expressions
66
+
## Usage guidelines
67
67
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)
69
70
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
71
72
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).
73
75
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.
75
77
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`.
77
79
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.
79
81
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.
81
83
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).
83
85
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.
85
87
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.
87
89
88
-
-`INTO`
90
+
The following clauses can't be used in the *CTE_query_definition*:
89
91
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`
91
96
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.
93
98
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.
107
100
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.
109
102
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.
111
104
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.
113
106
114
-
- The FROM clause of a recursive member must refer only one time to the CTE *expression_name*.
- The following items aren't allowed in the *CTE_query_definition* of a recursive member:
109
+
### Guidelines for recursive common table expressions
117
110
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).
119
113
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.
121
115
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.
123
117
124
-
-`HAVING`
118
+
The number of columns in the anchor and recursive members must be the same.
125
119
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.
127
121
128
-
-`TOP`
122
+
The FROM clause of a recursive member must refer only one time to the CTE *expression_name*.
129
123
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:
131
125
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*.
133
135
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).
135
137
136
138
The following guidelines apply to using a recursive common table expression:
137
139
138
140
- All columns returned by the recursive CTE are nullable regardless of the nullability of the columns returned by the participating `SELECT` statements.
139
141
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).
141
143
142
144
- A view that contains a recursive common table expression can't be used to update data.
143
145
@@ -173,7 +175,7 @@ The current implementation of CTEs in [!INCLUDE [ssazuresynapse-md](../../includ
173
175
174
176
- 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.
175
177
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.
177
179
178
180
- When a CTE is used in a statement that is part of a batch, the statement before it must be followed by a semicolon.
179
181
@@ -223,7 +225,7 @@ FROM Sales_CTE;
223
225
224
226
### C. Use multiple CTE definitions in a single query
225
227
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)].
227
229
228
230
```sql
229
231
WITH Sales_CTE (SalesPersonID, TotalSales, SalesYear)
@@ -394,7 +396,7 @@ FROM cte
394
396
OPTION (MAXRECURSION 2);
395
397
```
396
398
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.
398
400
399
401
```sql
400
402
WITH cte (EmployeeID, ManagerID, Title) AS
@@ -455,7 +457,7 @@ ORDER BY ComponentLevel, AssemblyID, ComponentID;
455
457
456
458
### F. Use a recursive CTE in an UPDATE statement
457
459
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' (ProductAssemblyID800)`. 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.
0 commit comments