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
Copy file name to clipboardExpand all lines: docs/nested_CTES.md
+11-7Lines changed: 11 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,13 @@ title: Nested CTES in TSQL, a treatise
3
3
sidebar_position: 6
4
4
---
5
5
6
-
# Nested CTES in TSQL, a treatise
6
+
# Nesting queries with WITH clauses in TSQL, a treatise
7
7
8
-
## Ask
8
+
## Background
9
+
10
+
### TL;DR
11
+
12
+
Allowing nested WITH statements (not in love with terminology, examples given below) would enable MSFT customers to fully take advantage of templating engines and their respective ecosystems. These templating engines are already very popular on non-MSFT database products.
9
13
10
14
Today in TSQL, the below example is not correct TSQL, but the following two are.
11
15
@@ -81,9 +85,9 @@ SELECT * FROM goku_outer
81
85
IMHO, this is excessive and it makes query injection via templating needlessly difficult, especially given that both nested subqueries and sequentially nested CTEs are already supported.
82
86
83
87
84
-
# Reasons (ordered by ease of explanation)
88
+
##Reasons (ordered by ease of explanation)
85
89
86
-
## 1. Everyone else does it
90
+
###1. Everyone else does it
87
91
88
92
The open-source data engineering ecosystem is growing at a breakneck speed -- especially tools that work with cloud data warehouses (e.g. [Airflow](https://airflow.apache.org/), [dbt](https://www.getdbt.com/), [SQLFluff](https://www.sqlfluff.com/), and [Great Expectations](https://greatexpectations.io/))
89
93
@@ -107,7 +111,7 @@ The below table shows which databases support this convention.
107
111
108
112
109
113
110
-
### Low-hanging, Syntactic Fruit
114
+
### 2. Low-hanging, Syntactic Fruit
111
115
112
116
Both nested subqueries and sequentially nested CTEs are already supported, so IMO, it isn't a large amount of effor to support this.
113
117
@@ -121,7 +125,7 @@ WITH goku_outer AS (
121
125
SELECT*FROM goku_outer
122
126
```
123
127
124
-
### more flexibility with SQL templating engines
128
+
### 3. more flexibility with SQL templating engines
125
129
126
130
Quote from Jacob Matson ([@matsonj](https://github.com/matsonj)):
127
131
@@ -166,4 +170,4 @@ In fact, the magic of dbt is just Jinja templating. dbt goes one step further an
166
170
167
171
If TSQL could allow wrapping arbitrary SELECT queries within a CTE, it would have the following effects on dbt custom adapter maintenance:
168
172
- dbt-sqlserver and dbt-synapse get the benefits of [ephemeral](https://docs.getdbt.com/docs/building-a-dbt-project/building-models/materializations#ephemeral) materializations and dbt tests without extra work
169
-
- dbt-msft users can take advantage of community-supported adapters with more confidence (many package maintainers make heavy use of CTE-query-wrapping)
173
+
- dbt-msft users can take advantage of community-supported adapters with more confidence (many package maintainers make heavy use of CTE-query-wrapping)****
0 commit comments