Feb
25
Written by:
Steve Gray
2/25/2011 8:42 AM
I’ve almost got the CTE syntax memorized; as I start to do more work on SQL 2005+. But not quite, I still have to Google to be sure I have it right. So, to make that easier, here is a simple example of CTE syntax to get the ball rolling.
Code Snippet
- 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
As always, I welcome your comments!