树型结构处理_双编号

/*
Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)   Jul  9 2008 14:43:34   Copyright (c)
1988-2008 Microsoft Corporation  Enterprise Evaluation Edition on Windows NT 5.1 <X86>
(Build 2600: Service Pack 3)
 愿和大家共同进步
如有雷同、实属巧合
●●●●●2009-09-03 17:47:36.077●●●●●
 ★★★★★soft_wsx★★★★★
*/
--树型结构处理之双编号(广度深度排序)
if OBJECTPROPERTY(object_id('tb'),'isusertable')<>0
  drop table tb
create table tb(ybh nvarchar(10),ebh nvarchar(10),beizhu nvarchar(1000))
insert tb
select '0001',null,'云南省'
union all select '0002','0001','昆明市'
union all select '0003','0001','昭通市'
union all select '0009','0001','大理市'
union all select '0008',null,'四川省'
union all select '0004',null,'贵州省'
union all select '0005','0002','五华区'
union all select '0007','0002','水富县'
union all select '0006','0005','西园路192号'
union all select '0010','0006','金色梧桐'
union all select '0011','0010','科技有限公司'
union all select '0015','0007','两碗乡'
union all select '0013','0015','两碗村'
union all select '0012','0013','某跨国集团董事长'
union all select '0014','0008','成都市'

--select * from tb
--广度排序(先显示第一层节点,再显示第二次节点......)
--定义辅助表
declare @level_tb table(bh nvarchar(10),level int)
declare @level int
set @level=0
insert @level_tb(bh,level)
select ybh,@level from tb where ebh is null
while @@ROWCOUNT>0
  begin
    set @level=@level+1
    insert @level_tb(bh,level)
      select ybh,@level
        from tb a,@level_tb b
        where a.ebh=b.bh
              and b.level=@level-1
  end
  select a.*,b.* from tb a,@level_tb b where a.ybh=b.bh order by level
/*
ybh ebh beizhu bh level
0001 NULL 云南省 0001 0
0008 NULL 四川省 0008 0
0004 NULL 贵州省 0004 0
0002 0001 昆明市 0002 1
0003 0001 昭通市 0003 1
0009 0001 大理市 0009 1
0014 0008 成都市 0014 1
0005 0002 五华区 0005 2
0007 0002 水富县 0007 2
0006 0005 西园路192号 0006 3
0015 0007 两碗乡 0015 3
0010 0006 金色梧桐 0010 4
0013 0015 两碗村 0013 4
0011 0010 科技有限公司 0011 5
0012 0013 某跨国集团董事长 0012 5
*/
 
  --深度排序(模拟单编码法)
   declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
  declare @level int
  set @level=0
  insert @level_tt(ybh,ebh,level)
  select ybh,ybh,@level from tb where ebh is null
  while @@ROWCOUNT>0
  begin
          set @level=@level+1
          insert @level_tt(ybh,ebh,level)
          select a.ybh,b.ebh+a.ybh,@level
            from tb a,@level_tt b
            where a.ebh=b.ybh and b.level=@level-1
 end
select space(b.level*2)+'----'+a.beizhu,a.*,b.*
  from tb a,@level_tt b
  where a.ybh=b.ybh
  order by b.ebh
/*(无列名) ybh ebh beizhu ybh ebh level
----云南省 0001 NULL 云南省 0001 0001 0
  ----昆明市 0002 0001 昆明市 0002 00010002 1
    ----五华区 0005 0002 五华区 0005 000100020005 2
      ----西园路192号 0006 0005 西园路192号 0006 0001000200050006 3
        ----金色梧桐 0010 0006 金色梧桐 0010 00010002000500060010 4
          ----科技有限公司 0011 0010 科技有限公司 0011 000100020005000600100011 5
    ----水富县 0007 0002 水富县 0007 000100020007 2
      ----两碗乡 0015 0007 两碗乡 0015 0001000200070015 3
        ----两碗村 0013 0015 两碗村 0013 00010002000700150013 4
          ----某跨国集团董事长 0012 0013 某跨国集团董事长 0012 000100020007001500130012 5
  ----昭通市 0003 0001 昭通市 0003 00010003 1
  ----大理市 0009 0001 大理市 0009 00010009 1
----贵州省 0004 NULL 贵州省 0004 0004 0
----四川省 0008 NULL 四川省 0008 0008 0
  ----成都市 0014 0008 成都市 0014 00080014 1
  */
 
 
 
  --查找子节点(包括本身节点和子节点)
 declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
  declare @level int
  set @level=0
  insert @level_tt(ybh,ebh,level)
  select ybh,ybh,@level from tb where ybh='0005'
  while @@ROWCOUNT>0
  begin
          set @level=@level+1
          insert @level_tt(ybh,ebh,level)
          select a.ybh,b.ebh+a.ybh,@level
            from tb a,@level_tt b
            where a.ebh=b.ybh and b.level=@level-1
 end
select space(b.level*2)+'----'+a.beizhu,a.*,b.*
  from tb a,@level_tt b
  where a.ybh=b.ybh
  order by b.ebh
 
 /*
 (无列名) ybh ebh beizhu ybh ebh level
----五华区 0005 0002 五华区 0005 0005 0
  ----西园路192号 0006 0005 西园路192号 0006 00050006 1
    ----金色梧桐 0010 0006 金色梧桐 0010 000500060010 2
      ----科技有限公司 0011 0010 科技有限公司 0011 0005000600100011 3
      */
 
  --查的父节点(包括本身节点和所有的你节点)
 declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
  declare @level int
  set @level=0
  insert @level_tt(ybh,ebh,level)
  select ybh,ebh,@level from tb where ebh='0005'
  while @@ROWCOUNT>0
  begin
          set @level=@level+1
          insert @level_tt(ybh,ebh,level)
          select a.ebh,b.ebh+a.ebh,@level
            from tb a,@level_tt b
            where a.ybh=b.ybh and b.level=@level-1
 end
select space(b.level*2)+'----'+a.beizhu,a.*,b.*
  from tb a,@level_tt b
  where a.ybh=b.ybh
  order by b.ebh desc
 
 /*
 (无列名) ybh ebh beizhu ybh ebh level
      ----云南省 0001 NULL 云南省 0001 0005000500020001 3
    ----昆明市 0002 0001 昆明市 0002 000500050002 2
  ----五华区 0005 0002 五华区 0005 00050005 1
----西园路192号 0006 0005 西园路192号 0006 0005 0
*/

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/soft_wsx/archive/2009/09/04/4521091.aspx

原文地址:https://www.cnblogs.com/lifuyun/p/lifuyun09092203.html