Hi,
The WITH Common Table Expression (CTE) is used for creating temporary
named result sets. The functionality was introduced in SQL Server 2005.
This T-SQL Expression begins with the keyword WITH. The
results from the WITH expression are stored in a temporary named result
set that can be queried. Using the WITH expression, allows for the
simplification of query logic, by allowing the separation of logic into
separate steps. The WITH expression has a few basic parts:
-
WITH [name of temporary resultset] (columns
in result set)
-
AS ( SQL Query Definition )
following the 'WITH' keyword is the name of the temporary result set
being created and the names of the columns in the result set.
Parentheses are placed around the column names.
following the 'AS' keyword will be the SQL Query (surrounded by
parentheses). The number of columns selected in the query must match the
number of columns listed in the Table Expression Definition (the
columns listed after name of resultset).
Ex:WITH Developers (Name,Salary)
AS
(
SELECT Name, Salary FROM Employees WHERE Position = 'Software
Developer'
)
SELECT * FROM Developers
If you run the above query, It will give only the two columns selected above,corresponding to the WHERE clause.
~Arun