再帰的な CTE クエリを記述しようとすると、次のエラーが発生し続けます。level, invalid column name
これはクエリです。どこが間違っていますか?
WITH OwnerHierarchy AS (
SELECT PairID,
ChildID,
ParentID,
0 AS level
FROM BusinessHierarchy
UNION ALL
SELECT e.PairID,
e.ChildID,
e.ParentID,
level + 1 AS level
FROM BusinessHierarchy AS e
JOIN BusinessHierarchy AS eh ON e.ParentID = eh.ParentID)
SELECT PairID,
ChildID,
ParentID,
level
FROM OwnerHierarchy AS OwnerHierarchy_1
ORDER BY level, ChildID, ParentID
これはms sqlサーバー2005です。