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

解决方案 »

  1.   

    举例:declare @a table (TC_Id int,TC_PID int,TC_Name varchar(200))
    insert @a values(1,0,'中国')
    insert @a values(2,0,'美国')
    insert @a values(3,0,'加拿大')
    insert @a values(4,1,'北京')
    insert @a values(5,1,'上海')
    insert @a values(6,1,'江苏')
    insert @a values(7,6,'苏州')
    insert @a values(8,7,'常熟')
    insert @a values(9,6,'南京')
    insert @a values(10,6,'无锡')
    insert @a values(11,2,'纽约')
    insert @a values(12,2,'旧金山')declare @tmp1 table (TC_Id int,TC_PID int,TC_Name varchar(200),lev int)
    insert @tmp1 select *,1 from @a where tc_ID=1
    while exists(select 1 from @a a,@tmp1 b where a.tc_pid=b.tc_ID and a.tc_ID not in (select tc_ID from @tmp1))
      insert @tmp1 select a.*,1 from  @a a,@tmp1 b where a.tc_pid=b.tc_ID and a.tc_ID not in (select tc_ID from @tmp1)
    select * from @tmp1
      

  2.   

    http://expert.csdn.net/Expert/topic/1375/1375432.xml?temp=.8570978
      

  3.   

    /*-- 得到指定id的子id列表 --*/
    --不包含排序字段的情况
    create function f_getchildid(@id int)
    returns @re table(id int)
    as
    begin
    insert into @re select id from tb where pid=@id
    while @@rowcount>0
    insert into @re select a.id 
    from aa inner join @re b on a.pid=b.id
    where a.id not in(select id from @re)
    return
    end
    go
      

  4.   

    调用上面的函数实现你的查询:select * from dbo.getchildid(0) a inner join b on a.id=b.a_id
      

  5.   

    zjcxc(邹建) 
    我看过你那篇树的文章,用你的给的例子和数据可以实现,
    可是我自己试着用到自己的数据和表就不行了,只出来了一层子节点
    能不能解释一下例子中那一段代码实现了递归?
    就是我用语言的递归就是
    function aa()
    {
    aa()
    }
    那数据库的递归的原理是什么呢?