Search this blog

Wednesday, June 3, 2009

Common Table Expressions in SQL

A common table expression (CTE) closely resembles a nonpersistent view. It is a temporary named result set that you define in your query that will be used by the FROM clause of the query. Each CTE is defined only once (but can be referred to as many times as it is in scope) and lives for as long as the query lives. You can use CTEs to perform recursive operations.

Here is the syntax to create a CTE:

WITH < name of your CTE>(< column names>)

AS

(

< actual query>

)


To Access CTE:

SELECT * FROM <name of your CTE>


No comments:

Post a Comment