zoukankan      html  css  js  c++  java
  • 树型结构处理_双编号 (Z)

      1 /*
      2 Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)   Jul  9 2008 14:43:34   Copyright (c)
      3 1988-2008 Microsoft Corporation  Enterprise Evaluation Edition on Windows NT 5.1 <X86>
      4 (Build 2600: Service Pack 3)
      5  愿和大家共同进步
      6 如有雷同、实属巧合
      7 ●●●●●2009-09-03 17:47:36.077●●●●●
      8  ★★★★★soft_wsx★★★★★
      9 */
     10 --树型结构处理之双编号(广度深度排序)
     11 if OBJECTPROPERTY(object_id('tb'),'isusertable')<>0
     12   drop table tb
     13 create table tb(ybh nvarchar(10),ebh nvarchar(10),beizhu nvarchar(1000))
     14 insert tb
     15 select '0001',null,'云南省'
     16 union all select '0002','0001','昆明市'
     17 union all select '0003','0001','昭通市'
     18 union all select '0009','0001','大理市'
     19 union all select '0008',null,'四川省'
     20 union all select '0004',null,'贵州省'
     21 union all select '0005','0002','五华区'
     22 union all select '0007','0002','水富县'
     23 union all select '0006','0005','西园路192号'
     24 union all select '0010','0006','金色梧桐'
     25 union all select '0011','0010','科技有限公司'
     26 union all select '0015','0007','两碗乡'
     27 union all select '0013','0015','两碗村'
     28 union all select '0012','0013','某跨国集团董事长'
     29 union all select '0014','0008','成都市'
     30 
     31 --select * from tb
     32 --广度排序(先显示第一层节点,再显示第二次节点......)
     33 --定义辅助表
     34 declare @level_tb table(bh nvarchar(10),level int)
     35 declare @level int
     36 set @level=0
     37 insert @level_tb(bh,level)
     38 select ybh,@level from tb where ebh is null
     39 while @@ROWCOUNT>0
     40   begin
     41     set @level=@level+1
     42     insert @level_tb(bh,level)
     43       select ybh,@level
     44         from tb a,@level_tb b
     45         where a.ebh=b.bh
     46               and b.level=@level-1
     47   end
     48   select a.*,b.* from tb a,@level_tb b where a.ybh=b.bh order by level
     49 /*
     50 ybh ebh beizhu bh level
     51 0001 NULL 云南省 0001 0
     52 0008 NULL 四川省 0008 0
     53 0004 NULL 贵州省 0004 0
     54 0002 0001 昆明市 0002 1
     55 0003 0001 昭通市 0003 1
     56 0009 0001 大理市 0009 1
     57 0014 0008 成都市 0014 1
     58 0005 0002 五华区 0005 2
     59 0007 0002 水富县 0007 2
     60 0006 0005 西园路192号 0006 3
     61 0015 0007 两碗乡 0015 3
     62 0010 0006 金色梧桐 0010 4
     63 0013 0015 两碗村 0013 4
     64 0011 0010 科技有限公司 0011 5
     65 0012 0013 某跨国集团董事长 0012 5
     66 */
     67  
     68   --深度排序(模拟单编码法)
     69    declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
     70   declare @level int
     71   set @level=0
     72   insert @level_tt(ybh,ebh,level)
     73   select ybh,ybh,@level from tb where ebh is null
     74   while @@ROWCOUNT>0
     75   begin
     76           set @level=@level+1
     77           insert @level_tt(ybh,ebh,level)
     78           select a.ybh,b.ebh+a.ybh,@level
     79             from tb a,@level_tt b
     80             where a.ebh=b.ybh and b.level=@level-1
     81  end
     82 select space(b.level*2)+'----'+a.beizhu,a.*,b.*
     83   from tb a,@level_tt b
     84   where a.ybh=b.ybh
     85   order by b.ebh
     86 /*(无列名) ybh ebh beizhu ybh ebh level
     87 ----云南省 0001 NULL 云南省 0001 0001 0
     88   ----昆明市 0002 0001 昆明市 0002 00010002 1
     89     ----五华区 0005 0002 五华区 0005 000100020005 2
     90       ----西园路192号 0006 0005 西园路192号 0006 0001000200050006 3
     91         ----金色梧桐 0010 0006 金色梧桐 0010 00010002000500060010 4
     92           ----科技有限公司 0011 0010 科技有限公司 0011 000100020005000600100011 5
     93     ----水富县 0007 0002 水富县 0007 000100020007 2
     94       ----两碗乡 0015 0007 两碗乡 0015 0001000200070015 3
     95         ----两碗村 0013 0015 两碗村 0013 00010002000700150013 4
     96           ----某跨国集团董事长 0012 0013 某跨国集团董事长 0012 000100020007001500130012 5
     97   ----昭通市 0003 0001 昭通市 0003 00010003 1
     98   ----大理市 0009 0001 大理市 0009 00010009 1
     99 ----贵州省 0004 NULL 贵州省 0004 0004 0
    100 ----四川省 0008 NULL 四川省 0008 0008 0
    101   ----成都市 0014 0008 成都市 0014 00080014 1
    102   */
    103  
    104  
    105  
    106   --查找子节点(包括本身节点和子节点)
    107  declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
    108   declare @level int
    109   set @level=0
    110   insert @level_tt(ybh,ebh,level)
    111   select ybh,ybh,@level from tb where ybh='0005'
    112   while @@ROWCOUNT>0
    113   begin
    114           set @level=@level+1
    115           insert @level_tt(ybh,ebh,level)
    116           select a.ybh,b.ebh+a.ybh,@level
    117             from tb a,@level_tt b
    118             where a.ebh=b.ybh and b.level=@level-1
    119  end
    120 select space(b.level*2)+'----'+a.beizhu,a.*,b.*
    121   from tb a,@level_tt b
    122   where a.ybh=b.ybh
    123   order by b.ebh
    124  
    125  /*
    126  (无列名) ybh ebh beizhu ybh ebh level
    127 ----五华区 0005 0002 五华区 0005 0005 0
    128   ----西园路192号 0006 0005 西园路192号 0006 00050006 1
    129     ----金色梧桐 0010 0006 金色梧桐 0010 000500060010 2
    130       ----科技有限公司 0011 0010 科技有限公司 0011 0005000600100011 3
    131       */
    132  
    133   --查的父节点(包括本身节点和所有的你节点)
    134  declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
    135   declare @level int
    136   set @level=0
    137   insert @level_tt(ybh,ebh,level)
    138   select ybh,ebh,@level from tb where ebh='0005'
    139   while @@ROWCOUNT>0
    140   begin
    141           set @level=@level+1
    142           insert @level_tt(ybh,ebh,level)
    143           select a.ebh,b.ebh+a.ebh,@level
    144             from tb a,@level_tt b
    145             where a.ybh=b.ybh and b.level=@level-1
    146  end
    147 select space(b.level*2)+'----'+a.beizhu,a.*,b.*
    148   from tb a,@level_tt b
    149   where a.ybh=b.ybh
    150   order by b.ebh desc
    151  
    152  /*
    153  (无列名) ybh ebh beizhu ybh ebh level
    154       ----云南省 0001 NULL 云南省 0001 0005000500020001 3
    155     ----昆明市 0002 0001 昆明市 0002 000500050002 2
    156   ----五华区 0005 0002 五华区 0005 00050005 1
    157 ----西园路192号 0006 0005 西园路192号 0006 0005 0
    158 */
    159   

    http://blog.csdn.net/soft_wsx/article/details/4521091

  • 相关阅读:
    JS中offsetTop、clientTop、scrollTop、offsetTop各属性介绍
    在MOSS中使用无刷新的日历日程控件
    VCalendar不错的开源日历项目
    非常适用的Exchange 2007 Web Services
    在C#中实现DateDiff功能
    Div被Select挡住的解决办法
    安装Project Server2007出现错误
    vs2005中调试js(转)
    CrystalReports在MOSS下的新问题:来自磁盘上的图片不能显示
    关于多级审批工作流的问题描述
  • 原文地址:https://www.cnblogs.com/ryhan/p/2879703.html
Copyright © 2011-2022 走看看