Saturday, August 25, 2012

CTE is MSSQL

A CTE stands for Common Table Expressions and is a way in MSSQL2005 or greater to do a select after a select without using a temp table. 4 Guys from Rolla offers this example:

WITH ProductAndCategoryNamesOverTenDollars (ProductName, CategoryName,
      UnitPrice) AS
(
   SELECT
      p.ProductName,
      c.CategoryName,
      p.UnitPrice
   FROM Products p
      INNER JOIN Categories c ON
         c.CategoryID = p.CategoryID
   WHERE p.UnitPrice > 10.0
)
SELECT *
FROM ProductAndCategoryNamesOverTenDollars
ORDER BY CategoryName ASC, UnitPrice ASC, ProductName ASC

No comments:

Post a Comment