TL;DR: nested WITH statements are supported in all modern data warehouses, lakehouses, and database engines except for Fabric & T-SQL products. This makes the usage of SQL templating engines unnecessarily complex.
Fabric supports regular WITH statements like the following:
with customers as ( select * from lakehouseone.dbo.customers ), orders as ( select * from lakehousetwo.dbo.orders ) select * from orders o join customers c on o.customer_id = c.id
However, the following nested version is currently not supported:
with customers_with_addresses as ( customers as ( select * from lakehouseone.dbo.customers ), addresses as ( select * from lakehousetwo.dbo.addresses ), final as ( select c.id, c.name, a.line_1, a.zip, a.country from customers c join addresses a on c.address_id = a.id ) select * from final ), orders as ( select * from lakehousetwo.dbo.orders ) select * from orders o join customers_with_addresses c on o.customer_id = c.id
You can see how working with CTEs allows for a very clean approach to SQL and enables the use of modular blocks of SQL.
This is especially popular in SQL templating engines like dbt.
Nested WITH statements allow for easy query injection. Wrapping a SELECT statement and inserting it into a query as a CTE only works if that inserted statement doesn't contain any CTEs.
More users have complained about the lack of nested WITH statements here and here.
3 Comments
- xiaoyuliNew Member
Thanks for your feedback. We are working on adding the support of nested CTE to Fabric Warehousing.
- sebgrosNew Member
Frameworks like DBT using jinja templating used nested WITH but Fabric Warehouse don’t support
this syntax and it prevents ephemeral materialization with DBT. Most of concurrent warehouses as welll
Spark SQL already support this syntax.
It would be great to support it
— Does not work in fabric T-SQL
WITH outer AS(
WITH inner AS(
SÉLECT * FROM t
)
SELECT * FROM inner
)
SELECT * FROM outer
- fbcideas_migusrNew MemberStatus added:Planned
Recent ideas
About "Download SQL database project" and fix the downloaded project
1. The sqlproj must be fixed, it is downloaded without the xml markup line <?xml version="1.0" encoding="utf-8"> 2. The sdk must be updated: Actual: <Sdk Name="Microsoft.Build.Sql" Version="0.1...Jarrinf2 hours agoFrequent VisitorNew48Views3likes2CommentsEventstream MQTT broker source: V3.1.1 support
The Eventstream MQTT broker source supports connections with public MQTT brokers that offer MQTT V5 support. Please offer support for V3.1.1 too, because this is still a valid and widely used ver...svelde3 hours agoSuper UserCompleted1.4KViews6likes4CommentsGet data from Apache Kafka topics via private networking
Current prerequisites limit to public Kafka instances which is not feasible for most enterprise customers. When will this limitation be lifted so that it can be used in private network setups, for ...Jugi3 hours agoMicrosoft EmployeePlanned3.5KViews2likes3CommentsSet default x axis scrolling position
Enable selection of the default scroll position for the x-axis on a chart. For example, on a line chart with dates as the x-axis, when not all of the data can display in the view at once, the scroll...Lisa_Krieg11 hours agoNew MemberNeeds Votes7.8KViews1154likes104Comments