sql树型应用总结

--测试数据
if object_id('[T]') is not null drop table [T]
create table [T] (ID int,PID int,NAME varchar(10))
insert into [T]
select 1,0,'A' union all
select 2,1,'B' union all
select 3,2,'C' union all
select 4,0,'D' union all
select 5,4,'E' union all
select 6,2,'F' union all
select 7,3,'G' union all
select 8,7,'H'
go

--获取所有的要根结点
select t.* from T t where  exists (select 1 from T s where s.pid = t.id) 
go

--获取所有的叶子结点
select t.* from T t where not exists (select 1 from T s where s.pid = t.id) 
go

--获取树型记录的全路径

if object_id('[dbo].[getALLPath]') is not null
drop function [dbo].[getALLPath]
go
create  function getALLPath()
returns   @t   table(id   int,pid   int,name varchar(50),path varchar(300))
as
begin
 insert into @t select id,pid,name,null as path from T
 update @t  set path=name
 DECLARE @i int,@j int
 set @i=0
 set @j=1
 select @i=max(pid) from @t
 while @j<=@i
 begin
     update v set  path=a.path+'.'+v.name
     from @t v inner join @t a on v.pid=a.id
     where v.pid=@j
     set @j=@j+1
 end
return
end
go

select * from getALLPath()
go

--获取树型记录的全路径
if object_id('[dbo].[getpath]') is not null
drop function [dbo].[getpath]
go
create  function getpath(@id varchar(20))
returns varchar(300)
as
begin
 declare @s varchar(300)
 select @s = name,@id = pid  from T where id = @id
 while exists (select 1 from T where id = @id )
   select @s = name+'.'+@s,@id = pid from T where id = @id
 return @s
end
go

select t.*,dbo.getpath(cast(id as varchar(20))) path from T
go

--查找树
if object_id('[dbo].[GetChild]') is not null
drop function [dbo].[GetChild]
go
create    function   GetChild(@id   int)  
returns   @returnT   table(pid   int,id   int,name varchar(50))  
AS  
begin  
  insert   into   @returnT   select   pid,id,name  from   T   where   id=@id 
  insert   into   @returnT   select   pid,id,name  from   T   where   pid=@id    
    while   @@rowcount>0  
      insert   into   @returnT   select   A.pid,A.id,A.name
      from   T   A   inner   join   @returnT   B   on   A.pid=B.id  
      where   A.id   not   in(select   id   from   @returnT)
   return 
end  
go

select * from GetChild(1)
go

--查找树型记录的全路径
if object_id('[dbo].[getFindPath]') is not null
drop function [dbo].[getFindPath]
go
create  function getFindPath(@parentId varchar(20))
returns   @returndepartment   table(id   int,pid   int,name varchar(50),path varchar(300))
as
begin
 declare @t   table(id   int,pid   int,name varchar(50),path varchar(300))
 insert into @t select id,pid,name,null as path from T
 update @t  set path=name
 DECLARE @i int,@j int
 set @i=0
 set @j=1
 select @i=max(pid) from @t
 while @j<=@i
 begin
     update v set  path=a.path+'.'+v.name
     from @t v inner join @t a on v.pid=a.id
     where v.pid=@j
     set @j=@j+1
 end
 insert   into   @returndepartment   select   *   from   @t   where id=@parentId
 insert   into   @returndepartment   select   *   from   @t   where pid=@parentId    
 while   @@rowcount>0  
     insert   into   @returndepartment   select   a.id,a.pid,a.name,a.path
     from   @t   A   inner   join   @returndepartment   B   on   A.pid=B.id 
     where   A.id   not   in(select   id   from   @returndepartment)    
return
end
go

select * from getFindPath(1)
go

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值