Pages

Men

rh

6/03/2012

With Common Table Expression in SQl Server

It specifies temporary named result set, known as Common Table Expression. This is derived from a simple Query and defined with in the execution scope of single SELECT, INSERT, UPDATE or DELETE statements. This class can also used in CREATE VIEW statement as a part of its defining SELECT statement.
A Common Table expression can include reference to itself. This is referred to as a recursive Common Table Expression.

Example:  Creating Simple Common Table Expression :-

With Sales_CTE(SalesPersonID, SalesOrderID, SalesYear)
AS
(
  SELECT
           SalesPersonID,
           SalesOrderID,
           Year(OrderDate) as SalesYear
  FROM
  Sales_CTE
  GROUP BY
           SalesYear,
           SalesOrderID
  ORDER BY
            SalesOrderID,
            SalesYear
)
GO
         

No comments :

Post a Comment