sql得到孩子节点列表

本文介绍了一种使用SQL语句处理树形结构数据的方法,并提供了一个具体的存储过程实例,通过递归方式获取指定节点的所有子节点,实现了父子节点数据的有序展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我在用sql语句编写存储过程时,得到孩子节点的语句很不好做,逻辑性比较强,csdn社区上答贴时也没有花更多时间写,今天做项目时遇到了,所以写了一个,与大家分享。

想要得到的效果:

得到的table中的记录是类似于树形结构,孩子节点紧随父节点如:

处理之前的数据:

ID          PID         LEVEL
----------- ----------- -----------
1           0           0
6           5           3
7           6           4
2           1           1
3           1           1
4           2           2
5           3           2
8           10          1
9           8           2
10          0           0
11          10          1

处理之后的数据:

ID         PID        LEVEL
---------- ---------- -----------
1          0          0
2          1          1
4          2          2
3          1          1
5          3          2
6          5          3
7          6          4
10         0          0
8          10         1
9          8          2
11         10         1

sql语句:

create table BOM(ID INT,PID INT,[LEVEL] INT)
insert into BOM select 1,0,0
insert into BOM select 6,5,3
insert into BOM select 7,6,4
insert into BOM select 2,1,1
insert into BOM select 3,1,1
insert into BOM select 4,2,2
insert into BOM select 5,3,2
insert into BOM select 8,10,1
insert into BOM select 9,8,2
insert into BOM select 10,0,0
insert into BOM select 11,10,1
go

SELECT * FROM BOM

ID          PID         LEVEL
----------- ----------- -----------
1           0           0
6           5           3
7           6           4
2           1           1
3           1           1
4           2           2
5           3           2
8           10          1
9           8           2
10          0           0
11          10          1

(11 row(s) affected)

 

--创建用户定义函数用于取每个父节点下子节点
create function f_getChild(@PID int)
returns @t table(ID VARCHAR(10),PID VARCHAR(10),[LEVEL] INT)
as
begin
    declare @i int
    declare @tb table(ID VARCHAR(10),PID VARCHAR(10),[LEVEL] INT)
    set @i = 0
    insert into @tb select ID,PID,[LEVEL] from BOM where PID = @PID
    declare @ID INT
    while @i < (select count(*) from @tb)
    begin
        declare @WID INT
        SET  @WID = (select top 1 ID from @tb where ID in (select top (@i+1) ID from @tb) order by ID DESC)
        IF(@WID=@ID)
        begin
           insert into @t
               select top 1 * from @tb where ID in (select top (@i+1) ID from @tb) order by ID ASC
           SET  @WID = (select top 1 ID from @tb where ID in (select top (@i+1) ID from @tb) order by ID ASC)
        end
        ELSE
        BEGIN
            insert into @t
               select top 1 * from @tb where ID in (select top (@i+1) ID from @tb) order by ID DESC
        END
     insert into @t select * from dbo.f_getChild(@WID)
        set @ID = @WID
        set @i = @i + 1
    end
    return
end
go

 

SELECT * FROM dbo.f_getChild(0)

------drop function f_getChild

ID         PID        LEVEL
---------- ---------- -----------
1          0          0
2          1          1
4          2          2
3          1          1
5          3          2
6          5          3
7          6          4
10         0          0
8          10         1
9          8          2
11         10         1

(11 row(s) affected)

 

如果想得到id为1的孩子列表: SELECT * FROM dbo.f_getChild(1)

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值