Anda dapat melakukannya dengan ekspresi tabel umum rekursif (cte).
WITH X (ProductId, CategoryId) AS (
SELECT ProductId, CategoryId FROM #ProductCategory
UNION ALL
SELECT X.ProductId, C.ParentCategoryId FROM X
INNER JOIN #Category C ON X.CategoryId = C.CategoryId
)
SELECT ProductId, CategoryId FROM X ORDER BY CategoryId, ProductId
Informasi lebih lanjut di http://msdn.microsoft.com/en-us/ library/ms186243.aspx