Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.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 server 父/子层次结构树视图_Sql Server_Treeview_Hierarchy - Fatal编程技术网

Sql server 父/子层次结构树视图

Sql server 父/子层次结构树视图,sql-server,treeview,hierarchy,Sql Server,Treeview,Hierarchy,我有一张父母的桌子,看起来像这样 CHILD_ID | PARENT_ID | NAME 1 | Null | Bill 2 | 1 | Jane 3 | 1 | Steve 4 | 2 | Ben 5 | 3 | Andrew Bill ---Jane ------Ben ---Steve ------Andrew 我想得到这样一个结果集 CHIL

我有一张父母的桌子,看起来像这样

CHILD_ID | PARENT_ID | NAME
1        | Null      | Bill
2        | 1         | Jane
3        | 1         | Steve
4        | 2         | Ben
5        | 3         | Andrew
Bill
---Jane
------Ben
---Steve
------Andrew
我想得到这样一个结果集

CHILD_ID | PARENT_ID | NAME
1        | Null      | Bill
2        | 1         | Jane
3        | 1         | Steve
4        | 2         | Ben
5        | 3         | Andrew
Bill
---Jane
------Ben
---Steve
------Andrew
我知道我需要做一个排名查询来对级别进行排名和一个自连接,但我在网上只能找到CTE递归


我以前在Oracle中做过这件事,但在MS SQL中没有做过,这有点不成熟,可以改进,但希望它能显示出原理

;with relation (childId, parentId, childName, [level], [orderSequence])  
as  
(  
select childId, parentId, childName, 0, cast(childId as varchar(20))  
from @parents  
where parentId is null  
union all  
select p.childId, p.parentId, r.[level]+1, cast(r.orderSequence + '_' + cast(p.childId as varchar) as varchar(20))  
from @parents p  
inner join relation r on p.parentId = r.childId  
)  

select right('----------', ([level]*3)) +childName  
from relation  
order by orderSequence
然而,如果您希望避免递归,那么另一种方法是使用相关的树结构信息实现一个树表-请参阅以了解详细信息

declare @pc table(CHILD_ID int, PARENT_ID int, [NAME] varchar(80));
 
insert into @pc
select 1,NULL,'Bill' union all
select 2,1,'Jane' union all
select 3,1,'Steve' union all
select 4,2,'Ben' union all
select 5,3,'Andrew' union all
select 6,NULL,'Tom' union all
select 7,8,'Dick' union all
select 8,6,'Harry' union all
select 9,3,'Stu' union all
select 10,7,'Joe';
 
 
; with r as (
      select CHILD_ID, PARENT_ID, [NAME], depth=0, sort=cast(CHILD_ID as varchar(max))
      from @pc
      where PARENT_ID is null
      union all
      select pc.CHILD_ID, pc.PARENT_ID, pc.[NAME], depth=r.depth+1, sort=r.sort+cast(pc.CHILD_ID as varchar(30))
      from r
      inner join @pc pc on r.CHILD_ID=pc.PARENT_ID
      where r.depth<32767
)
select tree=replicate('-',r.depth*3)+r.[NAME]
from r
order by sort
option(maxrecursion 32767);

这是一个艰难的时刻。我扩展了示例,使其包含>1棵树。到目前为止,结果看起来不错。

这是递归的,我得到了终止的语句。在语句完成之前,最大递归100已耗尽。它是递归的,因此如您所说,限制为100级
WITH DirectReports (ParentUniqId, UniqId, SortID)
AS
(
    SELECT e.ParentUniqId, e.UniqId, e.UniqId as SortID
    FROM Coding.Coding AS e
    WHERE isnull(ParentUniqId ,0)=0
    UNION ALL
    SELECT e.ParentUniqId, e.UniqId,, d.SortID * 100 + e.UniqId  as SortID
    FROM Coding.Coding AS e
    INNER JOIN DirectReports AS d
        ON e.ParentUniqId = d.UniqId

)
SELECT ParentUniqId, Perfix,SortID
FROM DirectReports order by rtrim(SortID) , uniqid