sql2000表如下
比如我单击"AAA",就列出"EEE","FFF","GGG,"HHH","NNN","JJJ"
单击"CCC",就列出"EEE","FFF,"GGG"
单击"III",列出"NNN"
单击"KKK",列出"LLL","MMM"
单击"BBB",列出"LLL","MMM"怪当初二叉树没学好头痛大家帮帮忙吧

解决方案 »

  1.   

    BOM--参考
    CREATE TABLE BOM(PID INT,ID INT)
    INSERT INTO BOM SELECT 801,101
    INSERT INTO BOM SELECT 801,102
    INSERT INTO BOM SELECT 801,103
    INSERT INTO BOM SELECT 801,601
    INSERT INTO BOM SELECT 601,101
    INSERT INTO BOM SELECT 601,105
    INSERT INTO BOM SELECT 601,501
    INSERT INTO BOM SELECT 501,106
    INSERT INTO BOM SELECT 501,121
    GOCREATE FUNCTION F_GETROOT(@PID INT)
    RETURNS INT
    AS
    BEGIN
        DECLARE @ID INT
        WHILE EXISTS(SELECT 1 FROM BOM WHERE ID=@PID)
        BEGIN
            SET @ID=@PID
            SELECT @PID=PID FROM BOM WHERE ID=@ID
        END
        RETURN @PID
    END
    GOSELECT PID=DBO.F_GETROOT(PID),ID FROM BOM
    GO/*
    PID         ID
    ----------- ----------- 
    801         101
    801         102
    801         103
    801         601
    801         101
    801         105
    801         501
    801         106
    801         121
    */
    DROP FUNCTION F_GETROOT
    DROP TABLE BOM
    GO
    --生成测试数据
    create table BOM_1(Item int,bom_head varchar(20),bom_child varchar(20),number int,products_attribute  varchar(20))
    insert into BOM_1 select 1 ,'A' ,'A1',1,'采购'
    insert into BOM_1 select 2 ,'A' ,'A2',2,'生产'
    insert into BOM_1 select 3 ,'A2','A3',3,'生产'
    insert into BOM_1 select 4 ,'A2','A4',2,'采购'
    insert into BOM_1 select 5 ,'A3','A5',2,'采购'
    insert into BOM_1 select 6 ,'A3','A6',1,'采购'
    insert into BOM_1 select 7 ,'B' ,'B1',1,'采购'
    insert into BOM_1 select 8 ,'B' ,'B2',2,'生产'
    insert into BOM_1 select 9 ,'B2','B3',3,'生产'
    insert into BOM_1 select 10,'B2','B4',2,'采购'
    insert into BOM_1 select 11,'B3','B5',2,'采购'
    insert into BOM_1 select 12,'B3','B6',2,'采购'
    go
       --创建用户定义函数,用于取每个父节点下子节点的采购配置信息
    create function f_stock(@bom_head varchar(20))
    returns @t table(bom varchar(20),number int)
    as
    begin 
        declare @level int
        declare @a table(bom varchar(20),number int,products_attribute varchar(20),[level] int)
        set @level=1    if exists(select 1 from BOM_1 where bom_head=@bom_head)    
        insert into @a 
        select bom_child,number,products_attribute,@level 
        from BOM_1 
        where bom_head=@bom_head
        
        while exists(select 1 from @a where [level]=@level and products_attribute='生产')
        begin
            set @level=@level+1
            insert into @a(bom,number,products_attribute,[level])
            select a.bom_child,a.number,a.products_attribute,@level 
            from BOM_1 a,@a b
            where a.bom_head=b.bom and b.[level]=@level-1
        end
        
        insert into @t(bom,number) select bom,number from @a where products_attribute='采购'
        return
    end
    go
    --执行调用,取父节点'A'一个标准配置分解的采购信息及数量
    select * from dbo.f_stock('A')
    --生成测试数据
    create table BOM(ID INT,PID INT,MSG VARCHAR(1000))
    insert into BOM select 1,0,NULL
    insert into BOM select 2,1,NULL
    insert into BOM select 3,1,NULL
    insert into BOM select 4,2,NULL
    insert into BOM select 5,3,NULL
    insert into BOM select 6,5,NULL
    insert into BOM select 7,6,NULL
    go--创建用户定义函数用于取每个父节点下子节点的采购配置信息
    create function f_getChild(@ID VARCHAR(10))
    returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
    as
    begin
        declare @i int
        set @i = 1
        insert into @t select ID,PID,@i from BOM where PID = @ID
        
        while @@rowcount<>0
        begin
            set @i = @i + 1
            
            insert into @t 
            select 
                a.ID,a.PID,@i 
            from 
                BOM a,@t b 
            where 
                a.PID=b.ID and b.Level = @i-1
        end
        return
    end
    go--执行查询
    select ID from dbo.f_getChild(3)
    go--输出结果
    /*
    ID
    ----
    5
    6
    7
    */--删除测试数据
    drop function f_getChild
    drop table BOM创建用户定义函数,每个子节点de父节点的信息
    --生成测试数据
    create table BOM(ID int,parentID int,sClassName varchar(10))
    insert into BOM values(1,0,'1111'      )
    insert into BOM values(2,1,'1111_1'    )
    insert into BOM values(3,2,'1111-1-1'  )
    insert into BOM values(4,3,'1111-1-1-1') 
    insert into BOM values(5,1,'1111-2'    )go--创建用户定义函数,每个子节点de父节点的信息
    create function f_getParent(@ID int)
    returns varchar(40)
    as
    begin
        declare @ret varchar(40)    while exists(select 1 from BOM where ID=@ID and parentID<>0)
        begin
            select @ID=b.ID,@ret=','+rtrim(b.ID)+isnull(@ret,'')
            from
                BOM a,BOM b
            where
                a.ID=@ID and b.ID=a.parentID
        end
        
        set @ret=stuff(@ret,1,1,'')
        return @ret
    end
    go--执行查询
    select ID,isnull(dbo.f_getParent(ID),'') as parentID from BOM
    go--输出结果
    /*
    ID          parentID                                 
    ----------- ---------------------------------------- 
    1           
    2           1
    3           1,2
    4           1,2,3
    5           1   
    */--删除测试数据
    drop function f_getParent
    drop table BOM
    go
      

  2.   

    --测试数据   深度排序     
      DECLARE   @t   TABLE(ID   char(3),PID   char(3),Name   nvarchar(10))   
      INSERT   @t   SELECT   '001',NULL   ,'山东省'   
      UNION   ALL   SELECT   '002','001','烟台市'   
      UNION   ALL   SELECT   '004','002','招远市'   
      UNION   ALL   SELECT   '003','001','青岛市'   
      UNION   ALL   SELECT   '005',NULL   ,'四会市'   
      UNION   ALL   SELECT   '006','005','清远市'   
      UNION   ALL   SELECT   '007','006','小分市'   
        
      --深度排序显示处理   
      --生成每个节点的编码累计(相同当单编号法的编码)   
      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   @t   
      WHERE   PID   IS   NULL   
      WHILE   @@ROWCOUNT>0   
      BEGIN   
      SET   @Level=@Level+1   
      INSERT   @t_Level   SELECT   a.ID,@Level,b.Sort+a.ID   
      FROM   @t   a,@t_Level   b   
      WHERE   a.PID=b.ID   
      AND   b.Level=@Level-1   
      END   
        
      --显示结果   
      SELECT   a.*   
      FROM   @t   a,@t_Level   b   
      WHERE   a.ID=b.ID   
      ORDER   BY   b.Sort   
      /*--结果   
      ID       PID       Name                 
      ------   ---------   ----------     
      001     NULL   山东省   
      002     001       烟台市   
      004     002       招远市   
      003     001       青岛市   
      005     NULL   四会市   
      006     005       清远市   
      007     006       小分市   
      --*/--查询指定节点及其所有子节点的函数   
      CREATE   FUNCTION   f_Cid(@ID   char(3))   
      RETURNS   @t_Level   TABLE(ID   char(3),Level   int)   
      AS   
      BEGIN   
      DECLARE   @Level   int   
      SET   @Level=1   
      INSERT   @t_Level   SELECT   @ID,@Level   
      WHILE   @@ROWCOUNT>0   
      BEGIN   
      SET   @Level=@Level+1   
      INSERT   @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   
      /*--结果   
      ID       PID     Name                 
      ------   -------   ----------     
      002     001     烟台市   
      004     002     招远市   
      --*/ --测试数据
    DECLARE @t TABLE(ID char(3),PID char(3),Name nvarchar(10))
    INSERT @t SELECT '001',NULL ,'山东省'
    UNION ALL SELECT '002','001','烟台市'
    UNION ALL SELECT '004','002','招远市'
    UNION ALL SELECT '003','001','青岛市'
    UNION ALL SELECT '005',NULL ,'四会市'
    UNION ALL SELECT '006','005','清远市'
    UNION ALL SELECT '007','006','小分市'--深度排序显示处理
    --生成每个节点的编码累计(相同当单编号法的编码)
    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 @t
    WHERE PID IS NULL
    WHILE @@ROWCOUNT>0
    BEGIN
        SET @Level=@Level+1
        INSERT @t_Level SELECT a.ID,@Level,b.Sort+a.ID
        FROM @t a,@t_Level b
        WHERE a.PID=b.ID
            AND b.Level=@Level-1
    END--显示结果
    SELECT SPACE(b.Level*2)+'|--'+a.Name
    FROM @t a,@t_Level b
    WHERE a.ID=b.ID
    ORDER BY b.Sort
    /*--结果
    |--山东省
      |--烟台市
        |--招远市
      |--青岛市
    |--四会市
      |--清远市
        |--小分市
    --*/
      

  3.   

    --bom结构,查找节点下所有子节点:create table os(id int,parentid int,desn varchar(10))
    insert into os select 1,0,'体育用品'
    insert into os select 2,0,'户外运动'
    insert into os select 3,1,'篮球'
    insert into os select 4,1,'足球'
    insert into os select 5,2,'帐篷'
    insert into os select 6,2,'登山鞋'
    insert into os select 7,0,'男士用品'
    insert into os select 8,7,'刮胡刀'
    insert into os select 9,3,'大号篮球'--求个节点下所有子节点:
    create function f_cid(@id int)
    returns varchar(500)
    as
    begin
         declare @t table(id int,parentid int,desn varchar(10),lev int)
         declare @lev int
         set @lev=1
         insert into @t select *,@lev from  os where id=@id
         while(@@rowcount>0)
         begin
              set @lev=@lev+1
              insert into @t select a.*,@lev from os a,@t b
              where a.parentid=b.id and b.lev=@lev-1
         end
         declare @cids varchar(500)
         select @cids=isnull(@cids+',','')+ltrim(id) from @t order by lev
         return @cids
    end
    go--调用函数
    select *,ids=dbo.f_cid(id) from os
    --得到每个节点路径:
    create proc wsp2
    @id int
    as
    select *,cast(' ' as varchar(10)) fullpath  into #os from os
    DECLARE @i int,@j int
    set @i=0
    set @j=1
    select @i=max(parentid) from #os
    update #os set fullpath=id 
    while @j<=@i
    begin
           update #os set fullpath=a.fullpath+','+ltrim(#os.id) 
                from #os inner join #os a on #os.parentid=a.id 
           where #os.parentid=@j 
           set @j=@j+1
    end
    select * from #os
    go
    --调用存储过程
    exec wsp2 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 --调用函数查询001(广东省)及其所有子节点 
    select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id 
    /* 
    id  pid  name      
    ---- ---- ---------- 
    001  NULL 广东省 
    002  001  广州市 
    003  001  深圳市 
    004  002  天河区 
    005  003  罗湖区 
    006  003  福田区 
    007  003  宝安区 
    008  007  西乡镇 
    009  007  龙华镇 
    010  007  松岗镇 (所影响的行数为 10 行) 
    */ --调用函数查询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 行) 
    */ --调用函数查询003(深圳市)及其所有子节点 
    select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id 
    /* 
    id  pid  name      
    ---- ---- ---------- 
    003  001  深圳市 
    005  003  罗湖区 
    006  003  福田区 
    007  003  宝安区 
    008  007  西乡镇 
    009  007  龙华镇 
    010  007  松岗镇 (所影响的行数为 7 行) 
    */ drop table tb 
    drop function f_cid
    ----------------------------------------------------------------------------------------------------------------
    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go
    ALTER trigger [tri_updatesalary_Mem_Type]
     on [dbo].[SalaryIncrease]
    after  insert
    as
     declare @i int
     set @i=@@identity
     
     update m
     set m.Mem_Type=case when s.SMark>500000 then '退休会员'--500000
                       when s.SMark>400000 then '钻石五级'--400000
                       when s.SMark>300000 then '钻石四级'--300000
                       when s.SMark>200000 then '钻石三级'--200000
                       when s.SMark>100000 then '钻石二级'--100000
                       when s.SMark>50000 then '钻石一级'--50000
                       when s.SMark>40000 then '五星级'--40000
                       when s.SMark>30000 then '四星级'--30000
                       when s.SMark>20000 then '三星级'--20000
                       when s.SMark>10000 then '二星级'--10000
                       when s.SMark>5000 then '一星级'--5000
                       else '一般VIP会员'
                   end
    from MemberInfo m
      join (select s.SCardName,sum(s.SMark) as SMark 
              from SalaryIncrease s join inserted i on s.SCardName=i.SCardName group by s.SCardName) s
        on m.Mem_Num=s.SCardName 
     
     --if exists(select * from MemberInfo m join inserted i on m.Mem_Num=i.SCardName and m.Mem_Mark>100 )
     if exists (select  m.SCardName,sum(m.ShopMark)as  from(select m.SCardName,m.ShopMark  from ShoppingMark m join inserted i on m.SCardName=i.SCardName
    where year(m.SDate)=year(getdate()) and month(m.SDate)=month(getdate())) m
     group by m.SCardName  having sum(m.ShopMark)>100)
     /**/
    /* --最新的(我写的,上面是整合魅影的)select * from ShoppingMark SM join (select m.SCardName,sum(m.ShopMark) as Mark from ShoppingMark m join inserted i on m.SCardName=i.SCardName group by m.SCardName) s
     on SM.SCardName=s.ScardName where month(SDate)=month(getdate()) and year(SDate)=year(getdate()) and s.Mark>100*/
    begin
     update s
       set s.SIncease=case
                          --积分大于1000就是星级.所以不用判断是否是星级
                       when m.Mem_Type<>'一般VIP会员' then s.SMark*0.3
                       else case 
                              when s.SMark>4000 then 
                                   s.SMark*0.3
                            --  when s.SMark>4000 then 
                                 --  200*0.2+200*0.23+200*0.25+200*0.28+(s.SMark-800)*0.3
                              when s.SMark>3000 then 
                                   1000*0.2+1000*0.23+1000*0.25+(s.SMark-600)*0.28
                              when s.SMark>2000 then 
                                   1000*0.2+100*0.23+(s.SMark-400)*0.25  
                              when s.SMark>1000 then
                                   (s.SMark-200)*.023+1000*0.2
                              else  s.SMark*0.2
                           end
                      end
     from SalaryIncrease as s
     join inserted i
        on s.SCardName=i.SCardName
      join MemberInfo m
        on (i.SCardName=m.Mem_Num and s.SID=@i) or (i.SCardName=m.Mem_Num and s.SIncease=0)
        end
    --go
      

  4.   

    其实你这是一个表设计的问题。
    --3个字段分别为:节点代码,父节点,节点名称。
    如果节点为顶级的话,PID为NULL.
    这样你在点开一个节点的话,取pid=这个节点的就OK了。
    一级一级的点开。--相关表结构及数据
    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' , '松岗镇') 
      

  5.   

    我有自己整理 2000-2005的 哼哼 --测试数据
    if OBJECT_ID('tb') is not null 
    drop table tb 
    go
    CREATE TABLE tb(ID char(3),PID char(3),Name nvarchar(10))
    INSERT tb SELECT '001',NULL ,'山东省'
    UNION ALL SELECT '002','001','烟台市'
    UNION ALL SELECT '004','002','招远市'
    UNION ALL SELECT '003','001','青岛市'
    UNION ALL SELECT '005',NULL ,'四会市'
    UNION ALL SELECT '006','005','清远市'
    UNION ALL SELECT '007','006','小分市'
    GO
    --2000的方法--查询指定节点及其所有子节点的函数
    CREATE FUNCTION f_Cid(@ID char(3))
    RETURNS @t_Level TABLE(ID char(3),Level int)
    AS
    BEGIN
    declare @Level int
    set @level=1
    insert @t_level select @id,@level
    while @@rowcount>0
    begin 
        set @level=@level+1
        insert @t_Level select tb.id,@level
         from tb join @t_level t on tb.pid=t.id
         where t.level+1=@level
       end
       return
    endselect tb.*
    from tb join dbo.f_cid('002') b
    on tb.ID=b.id
    /*
    ID   PID  Name
    ---- ---- ----------
    002  001  烟台市
    004  002  招远市*/
    go
    --2005的方法(CTE)declare @n varchar(10)
    set @n='002'
    ;with 
    jidian as
    (
      select * from tb where ID=@n
      union all
      select t.* from jidian j join tb t on j.ID=t.PID
    )
    select * from jidian 
    go
    /*
    ID   PID  Name
    ---- ---- ----------
    002  001  烟台市
    004  002  招远市
    */
    go
    --查找指定节点的所有父节点(标准树形,即一个子节点只有一个父节点)
    CREATE FUNCTION f_Pid(@ID char(3))
    RETURNS @t_Level TABLE(ID char(3))
    AS
    BEGIN
    INSERT @t_Level SELECT @ID
    SELECT @ID=PID FROM tb
    WHERE ID=@ID
    AND PID IS NOT NULL
    WHILE @@ROWCOUNT>0
    BEGIN
    INSERT @t_Level SELECT @ID
    SELECT @ID=PID FROM tb
    WHERE ID=@ID
    AND PID IS NOT NULL
    END
    RETURN
    END
    select tb.*
    from tb join dbo.f_Pid('004') b
    on tb.ID=b.id
    /*
    ID   PID  Name
    ---- ---- ----------
    001  NULL 山东省
    002  001  烟台市
    004  002  招远市
    */
    go
    --2005的方法
    declare @n varchar(10)
    set @n='004'
    ;with
    fujidian as
    (
     select * from tb where ID=@n and PID is not null
     union all
     select a.* from tb a join fujidian f on a.ID=f.PID 
    )
    select * from fujidian order by ID
    /*
    ID   PID  Name
    ---- ---- ----------
    001  NULL 山东省
    002  001  烟台市
    004  002  招远市
    */
      

  6.   

    aaaaaaaaaaaaaaaaaa~~~~~~~~~~~~~不要父节点。只要子节点啊~~~~~~~~~~
      

  7.   

    查找子节点--树型结构处理之双编号(广度深度排序)
    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','金色梧桐3-702'
    union all select '0011','0010','d空科技有限公司'
    union all select '0015','0007','两碗乡'
    union all select '0013','0015','两碗村'
    union all select '0012','0013','某跨国集团董事长家'
    union all select '0014','0008','成都市'=
      
      
      --深度排序(模拟单编码法)
       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
      
      
      ------------查找子节点(包括本身节点和子节点)--------------
     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
      

  8.   

    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','金色梧桐3-702'
    union all select '0011','0010','d空科技有限公司'
    union all select '0015','0007','两碗乡'
    union all select '0013','0015','两碗村'
    union all select '0012','0013','某跨国集团董事长家'
    union all select '0014','0008','成都市'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 as 名称,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
        ----金色梧桐3-702 0010 0006 金色梧桐3-702 0010 000500060010 2
          ----d空科技有限公司 0011 0010 d空科技有限公司 0011 0005000600100011 3
          */
      

  9.   


    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }    private void BindData()
        {
            DataTable dt = pv.GetUserRole(base.GetUserCode);//这里直接查询那个表,得到集合。通过select * from 表名
            CreateTreeViewRecursive(TreeView1.Nodes, dt, 0);
        }
        /// <summary>
        /// 递归查询数据,创建 TreeNode 节点
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="dataSource"></param>
        /// <param name="parentId"></param>
        private void CreateTreeViewRecursive(TreeNodeCollection nodes, DataTable dataSource, int parentId)
        {
            string fliter = String.Format("tree7={0}", parentId);
            // 查询出节点数据
            DataRow[] drArr = dataSource.Select(fliter);        TreeNode node;
            foreach (DataRow dr in drArr)
            {
                node = new TreeNode();
                // 设置属性 
                  node.Text = dr["name7"].ToString();
                int nodeId = (int)dr["id7"]; 
                nodes.Add(node);
               // 递归创建子节点
                 CreateTreeViewRecursive(node.ChildNodes, dataSource, nodeId);
            }
        }
      

  10.   

    解决了,多谢大家的热心帮忙,特别是soft_wsx的十八掌,和fredrickhu的强力沙发以下为我修改的代码
    alter proc show_final_nodes@node7 int
    asdeclare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
      declare @level int
      set @level=0
      insert @level_tt(ybh,ebh,level)
      select id7,id7,@level from sort_top where id7=@node7
      while @@ROWCOUNT>0
      begin 
              set @level=@level+1
              insert @level_tt(ybh,ebh,level)
              select a.id7,b.ebh+a.id7,@level
                from sort_top a,@level_tt b
                where a.tree7=b.ybh and b.level=@level-1
     endselect a.*,b.*
      from sort_top a,@level_tt b
      where a.id7=b.ybh and a.id7 in(select id7 from sort_top where id7 not in
    (select distinct tree7 from sort_top )
    )
      order by b.ebh
    godeclare @node7 int
    set @node7=2
    exec show_final_nodes @node7