| CTE with multiple AS statements With common table expressions, it is possible to create multiple queries using comma-separated AS statements. A query can then reference any or all of those queries in many different ways, even joining them. Syntax - 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. |