Common Table Expressions

Download sql-server eBook


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


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.

Related Examples


333 Contributors: 10
Wednesday, April 5, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial:

Download eBook