SQL SERVER级联查询及数据结构《存储过程-递归树形查询》
阅读原文时间:2023年07月09日阅读:1

--创建表,插入数据

create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null  , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go

--查询指定节点及其所有子节点的函数(存储过程)
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
  declare @level int
  set @level = 1
  insert into @t_level select @id , @level
  while @@ROWCOUNT > 0
  begin
    set @level = @level + 1
    insert into @t_level select a.id , @level
    from tb a , @t_Level b
    where a.pid = b.id and b.level = @level - 1
  end
  return
end
go

--调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id   pid  name       
---- ---- ---------- 
002  001  广州市
004  002  天河区

(所影响的行数为 2 行)
*/

--查询指定节点及其所有父节点的函数(存储过程)
create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))
as
begin
  insert into @t_level select @id
  select @id = pid from tb where id = @id and pid is not null
  while @@ROWCOUNT > 0
  begin
    insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null
  end
  return
end
go

--调用函数查询008(西乡镇)及其所有父节点
select a.* from tb a , f_pid('008') b where a.id = b.id order by a.id
/*
id   pid  name       
---- ---- ---------- 
001  NULL 广东省
003  001  深圳市
007  003  宝安区
008  007  西乡镇

(所影响的行数为 4 行)
*/

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

--上面的用户定义函数可以处理一个节点有多个父节点的情况,对于标准的树形数据而言,由于每个节点仅有一个父节点,所以也可以通过下面的用户定义函数实现查找标准树形数据的父节点。

--创建表,插入数据

create table tb1(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb1 values('001',NULL ,'广西省')
insert into tb1 values('002','001','南宁市')
insert into tb1 values('004','002','兴宁区')
insert into tb1 values('003','001','桂林市')
insert into tb1 values('005',NULL ,'广东省')
insert into tb1 values('006','005','深圳市')
insert into tb1 values('007','006','宝安区')
insert into tb1 values('008','007','龙岗镇')
insert into tb1 values('009','005','南山区')

--深度排序显示处理
--生成每个节点的编码累计(相同当单编号法的编码)
DECLARE @t_Level TABLE(ID char(3),Level int,Sort varchar(8000))
DECLARE @Level int
SET @Level=0
INSERT @t_Level SELECT ID,@Level,ID
FROM tb1
WHERE PID IS NULL
WHILE @@ROWCOUNT>0
BEGIN
    SET @Level=@Level+1
    INSERT @t_Level SELECT a.ID,@Level,b.Sort+a.ID
    FROM tb1 a,@t_Level b
    WHERE a.PID=b.ID
        AND b.Level=@Level-1
END

--显示结果
SELECT SPACE(b.Level*4)+'|----'+a.Name
FROM tb1 a,@t_Level b
WHERE a.ID=b.ID
ORDER BY b.Sort

地名
|----广西省
    |----南宁市
        |----兴宁区
    |----桂林市
|----广东省
    |----深圳市
        |----宝安区
            |----龙岗镇
    |----南山区

---------------------------------------------------------------------------------------------------------------------------------