Microsoft SQL ServerCommon Table Expressions


  • 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.