Puoi farlo con un'espressione di tabella comune ricorsiva (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
Ulteriori informazioni su http://msdn.microsoft.com/en-us/ libreria/ms186243.aspx