Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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
递归CTE在SQL Server中查找层次结构中的第一个管理器_Sql_Sql Server_Recursion_Common Table Expression - Fatal编程技术网

递归CTE在SQL Server中查找层次结构中的第一个管理器

递归CTE在SQL Server中查找层次结构中的第一个管理器,sql,sql-server,recursion,common-table-expression,Sql,Sql Server,Recursion,Common Table Expression,我有一张用户表 我想通过每个用户的工作位置层次结构为他们找到第一个经理 我设法写了这个脚本,但似乎无法使它正常工作。我需要用户1003必须有1001 managerId。现在显示1002 您只需使用manager的userId设置managerId,然后将managerId复制到链接记录 select 1001 as userid, 'L1' as locationCode, NULL as parentLocationCode, 1 as isManager into #Users uni

我有一张用户表

我想通过每个用户的工作位置层次结构为他们找到第一个经理

我设法写了这个脚本,但似乎无法使它正常工作。我需要用户1003必须有1001 managerId。现在显示1002


您只需使用manager的userId设置managerId,然后将managerId复制到链接记录

select 1001 as userid, 'L1' as locationCode, NULL as parentLocationCode, 1 as isManager into #Users union all
select 1002 as userid, 'L2' as locationCode, 'L1' as parentPad, 0 as isManager union all
select 1003 as userid, 'L3' as locationCode, 'L2' as parentPad, 0 as isManager 
;

WITH
cte_org AS (
    SELECT       
        d.userid 
        ,d.locationCode
        ,d.parentLocationCode
        ,d.isManager
        ,d.userid as managerId
    FROM       
        #Users as d
    WHERE d.parentLocationCode is NULL
    UNION ALL
    SELECT 
        d.userid 
        ,d.locationCode
        ,d.parentLocationCode
        ,d.isManager
        ,o.managerId as managerId
    FROM 
        #Users as d
        inner JOIN cte_org o on d.parentLocationCode=o.locationCode
)
SELECT *
FROM cte_org
OPTION (MAXRECURSION 32767);

userid  locationCode    parentLocationCode  isManager   managerId
1001    L1                                  1           1001
1002    L2              L1                  0           1001
1003    L3              L2                  0           1001

您只需使用manager的userId设置managerId,然后将managerId复制到链接记录

select 1001 as userid, 'L1' as locationCode, NULL as parentLocationCode, 1 as isManager into #Users union all
select 1002 as userid, 'L2' as locationCode, 'L1' as parentPad, 0 as isManager union all
select 1003 as userid, 'L3' as locationCode, 'L2' as parentPad, 0 as isManager 
;

WITH
cte_org AS (
    SELECT       
        d.userid 
        ,d.locationCode
        ,d.parentLocationCode
        ,d.isManager
        ,d.userid as managerId
    FROM       
        #Users as d
    WHERE d.parentLocationCode is NULL
    UNION ALL
    SELECT 
        d.userid 
        ,d.locationCode
        ,d.parentLocationCode
        ,d.isManager
        ,o.managerId as managerId
    FROM 
        #Users as d
        inner JOIN cte_org o on d.parentLocationCode=o.locationCode
)
SELECT *
FROM cte_org
OPTION (MAXRECURSION 32767);

userid  locationCode    parentLocationCode  isManager   managerId
1001    L1                                  1           1001
1002    L2              L1                  0           1001
1003    L3              L2                  0           1001