SQL Common Table Expression (SQL CTE) on SQL Server
One of the new features in SQL Server 2005 for t-sql developers is SQL CTE (Common Table Expression).
SQL Server Common Table Expressions - CTE, is named as temporary named result set in SQL Server 2005 Books Online.
Please note that most of the sql CTE examples are using data from AdventureWorks sample database build for SQL Server 2005.
SQL developers can download SQL Server 2005 AdventureWorks sample database from CodePlex.
SQL Common Table Expressions - CTE Syntax
A basic Common Table Expression, SQL CTE syntax is as follows:
The SQL Server CTE name has the rules that applies to sql table names.
If you want to use SPACE character in the CTE name, then you should surround the CTE name with "[" and "]" characters.
SQL Common Table Expression Sample - SQL CTE Sample
Here is a sql CTE sample for t-sql developers.
Please note that you can omit the column names following the t-sql CTE name.
These column name rename the output result set of the CTE select.
For example, the following CTE SQL Server query selects EmployeeID column but when you display contents of the CTE expression, you will see that the values are displayed under column named Id.
The above CTE sql sample is showing that Common Table Expressions (CTE) can be used as a simple sql view.
Common Table Expression (CTE) as SubSelect
A common table expression, CTE sql expression is basically a t-sql subquery statement in the main sql statement.
Let's consider the below example select CTE SQL Server statements.
The same result can be queried without using sql CTE expression as in the following subselect statement.
An important point about the above two CTE queries is that the actual execution plan of both are identical.
Multiple CTE in SQL Server 2005
Another feature in SQL CTE is CTE expressions can be used in cascade mode.
This means a CTE expression can be an input of an other SQL Server CTE expression.
Here is an other SQL Server multiple CTE example code.
You can see that there is two sql CTE expressions in the above single t-sql SELECT statement.
SQL developers can also refer to SQL Multiple CTE Syntax and T-SQL CTE Example in SQL Server 2008
SQL Server Recursive Common Table Expressions - Recursive CTE
I'm sure sql developers might not be impressed by the sql CTE Common Table Expressions according to the above t-sql examples.
In fact the read advantage of SQL Server Common Table Expressions CTE, is in sql statements where CTE is referencing to itself in the CTE data.
Such CTE queries are called SQL Server recursive common table expressions
Here is a sample SQL recursive CTE.
The SQL Server Recursive CTE queries are formed of two parts.
The first part is called anchor select.
Here is the anchor select of the above t-sql cte expression.
This is the start of SQL select statements.
This is the first row selected by recursive CTE which forms the result set.
The second part is the recursion part of the CTE expression.
It refers to the CTE itself.
Here is the recursive part of the sql CTE statement.
Note that the above query refers to CTE itself and fetch data from Employee table which is joined by a INNER JOIN.
The following recursive CTE query will return employees and their title who are working under the node of a specific manager in organizational chart.
Using this t-sql recursive CTE query with @Employee parameter, sql developers can query all employees in the hierarchy of the organizational chart.
A Recursive SQL CTE expression can be used as a sql hierarchical query.
SQL developers can use this sql recursive CTE query structure with small modifications on different sql hierarchical query structures refering to the same table.
For example, sql programmers can refer to SQL Recursive Function Sample - MS SQL Server Recursive T-SQL Split Function
One last important note on SQL Server CTE expressions syntax is that before the CTE select statement the last sql statement should be terminated with ";".
After ";", you can code your sql CTE statement otherwise.
If there is not a ";" between the previous sql statement and the tsql CTE statement, the following error will occur:
Msg 319, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
You can over come this sql error by starting the sql codes of the CTE - Common Table Expression as ";WITH"