Microsoft SQL Server Common Table Expressions

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • WITH cte_name [(column_name_1, column_name_2, ...)] AS (cte_expression)

Remarks

It is necessary to separate a CTE from the previous statement with a semi-colon (;) character.

i.e. ;WITH CommonTableName (...) SELECT ... FROM CommonTableName ...

A CTE's scope is a single batch, and only downstream of its definition. A batch may contain multiple CTEs, and a CTE may reference another CTE defined earlier in the batch, but a CTE may not reference another CTE that is defined later in the batch.



Got any Microsoft SQL Server Question?