/ Published in: SQL
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
WITH RecursiveCategories (CategoryHint, CategoryID, CatName, CatLevel, SearchKeywords, ParentCategoryId, ePagesOID) AS ( SELECT I.CategoryHint, C.CategoryId, C.Name AS CatName, C.CatLevel, C.SearchKeywords, C.ParentCategoryId, I.ePagesOID FROM Core.Inventory I LEFT JOIN Core.ProductBase PB ON PB.ProductId = I.ProductId LEFT JOIN Core.ProductCategory PC ON PC.ProductId = PB.ProductId LEFT JOIN Core.Category C ON C.CategoryId = PC.CategoryID WHERE I.ePagesOID = @ePagesId UNION ALL SELECT RC.CategoryHint, C.CategoryId, C.Name AS CatName, C.CatLevel, C.SearchKeywords, C.ParentCategoryId, RC.ePagesOID FROM Core.Category C JOIN RecursiveCategories RC ON C.CategoryID = RC.ParentCategoryID WHERE RC.ePagesOID = @ePagesId AND RC.ParentCategoryId IS NOT NULL ) SELECT CategoryHint, CatName, CatLevel, SearchKeywords, CategoryId, ParentCategoryId FROM RecursiveCategories ORDER BY CatLevel
URL: http://www.sqlservercentral.com/articles/Development/recursivequeriesinsqlserver2005/1760/