http://expert.csdn.net/Expert/topic/2285/2285830.xml?temp=.1570551/*--树形数据处理方案 树形数据的排序,新增,修改,复制,删除,数据完整性检查,汇总统计
--邹建 2003.9--*/

解决方案 »

  1.   

    http://expert.csdn.net/Expert/topic/2285/2285830.xml?temp=.1570551
      

  2.   

    好象此方案不能解决我的问题:我再放一组数据请大虾不吝赐教.
    FuncNo               ParentNo             
    -------------------- -------------------- 
    A                    
    B                    
    3A                   A
    C                    A
    55                   B
    77                   B
    MM                   B
    HB                   3A
    BB                   3A
    DD                   C
    FF                   C
    AH                   55
    7B                   55
    4A                   77
    8F                   77
    GG                   77
    LL                   MM
    H                    DD
    3C                   7B
                
    也就是树状结构,由于不好画,你可以试着画一下,A,B为最头结点其实我的数据并不是真的数据,这只是我测试用的数据,其是就是一种遍历的问题.我需要的结果一定是要根据FuncNo和ParentNo的数据来进行排序,因为这些测试数据并不是真的数据它会改变,只能根据FuncNo和ParentNo的数据来得到结果,谢谢关注.
    FuncNo               ParentNo             
    -------------------- --------------------
    A                    
    3A                   A
    HB                   3A
    BB                   3A
    C                    A
    DD                   C
    H                    DD
    FF                   C
    B                   
    55                   B
    AH                   55
    7B                   55
    3C                   7B
    77                   B
    4A                   77 
    8F                   77
    GG                   77
    MM                   B
    LL                   MM
      

  3.   

    ParentNo的值代表这条记录的父节点是哪个记录的FuncNo值.
      

  4.   

    建议用两个表实现,一个是所有节点列表,一个是节点结构表,节点列表:
    id   int(自动增加)
    aaa  varchar(节点名称)节点结构表:
    id   int(自动增加)
    id2  int(节点列表的id)
    bbb  varchar(子节点名称) or id3 int(子节点id)这种结构比较合理,查询也比较方便,而且可以实现无级扩展
      

  5.   

    set nocount on
    create table t(FuncNo   varchar(10),ParentNo  varchar(10))insert t select 'A',null  
    union all select 'B',null 
    union all select '3A','A'
    union all select 'C','A'
    union all select '55','B'
    union all select '77','B'
    union all select 'MM','B'
    union all select 'HB','3A'
    union all select 'BB','3A'
    union all select 'DD','C'
    union all select 'FF','C'
    union all select 'AH','55'
    union all select '7B','55'
    union all select '4A','77'
    union all select '8F','77'
    union all select 'GG','77'
    union all select 'LL','MM'
    union all select 'H','DD'
    union all select '3C','7B'/*=========================================================*/select identity(int,1,1) as id,cast('' as varchar(100)) as id2,* into #t from t where ParentNo is null order by FuncNoupdate #t set id2='000'+rtrim(id)while exists
    (
    select 1 
    from t A,#t B 
    where B.FuncNo=A.ParentNo 
    and not exists(
                  select 1 
                  from #t 
                  where ParentNo=A.parentNo 
                  and FuncNo=A.FuncNo)
    )insert #t select id2+right('000'+rtrim(No),4),
                     FuncNo,
                     ParentNo 
               from (
                   select A.*,
                          B.id2,
                         (select count(*) as No 
                          from t 
                          where ParentNo=A.ParentNo 
                          and FuncNo<=A.FuncNo) No 
                    from t A,#t B 
                    where B.FuncNo=A.ParentNo 
                    and not exists (select 1 
                                    from #t 
                                    where ParentNo=A.parentNo 
                                    and FuncNo=A.FuncNo)
                        ) Cselect FuncNo,ParentNo from #t order by id2drop table #t
    drop table t/*=========================================================
    FuncNo     ParentNo   
    ---------- ---------- 
    A          NULL
    3A         A
    BB         3A
    HB         3A
    C          A
    DD         C
    H          DD
    FF         C
    B          NULL
    55         B
    7B         55
    3C         7B
    AH         55
    77         B
    4A         77
    8F         77
    GG         77
    MM         B
    LL         MM
    ==========================================================*/