尝试编写递归 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 server 2005。