Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL中的递归CTE_Sql_Sql Server_Common Table Expression_Recursive Query - Fatal编程技术网

SQL中的递归CTE

SQL中的递归CTE,sql,sql-server,common-table-expression,recursive-query,Sql,Sql Server,Common Table Expression,Recursive Query,Union all函数在递归CTE内不工作 with CTE_Manager(id,manager,man_id,[Level]) as ( select id,manager,man_id,1 from manager where man_id is null union all select a.id,a.manager,a.man_id,b.[Level]+1 from manager a join CTE_Manager b o

Union all函数在递归CTE内不工作

with CTE_Manager(id,manager,man_id,[Level])
as
(
    select id,manager,man_id,1
    from manager
    where man_id is null

union all

    select a.id,a.manager,a.man_id,b.[Level]+1
    from manager a
    join CTE_Manager b
    on b.man_id= a.id
)
select a.manager,ISNULL(a.manager,'SUPER BOSS'),b.Level
from CTE_Manager a
join CTE_Manager b
on a.man_id=b.id
实际上,我得到了输出:


我在union all函数之前检索值。我必须从递归CTE获取所有值。

您的联接中的on子句是错误的。它应该是b.id=a.man\u id


您所做的是选择所有没有经理的经理,然后尝试查找他们的经理。当我怀疑你想要的是他们所有的下属时。

你的问题是什么?你说的“联盟都不起作用”是什么意思?你到底想达到什么目的?向我们展示一些示例数据和预期输出(编辑您的问题,不要在评论中发布)