Du kan göra detta med ett rekursivt vanligt tabelluttryck (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
Mer information på http://msdn.microsoft.com/en-us/ library/ms186243.aspx