我也知道递归的解法,但是mvm说了不能用递归,也不可以用临时表,那还有什么办法呢?

解决方案 »

  1.   

    --贴一个例子,可以参考下!(函数递归)请教一个较难的SQL语句
    表情况为
    ID  姓名  上级ID
    1   A      0
    2   B      1
    3   C      1
    4   D      2
    5   E      3
    6   F      4
    7   G      6
    我现在要做的是,输入一个ID号,要能得到它自己以及所有下级的ID号,例如输入1得到1,2,3,4,5,6,7;输入2得到2,4,6,7;输入3得到3,5create table A
    (
      ID int,
      [姓名] varchar(10),
      [上级ID] int
    )
    insert A
    select 1,'A',0 union
    select 2,'B',1 union
    select 3,'C',1 union
    select 4,'D',2 union
    select 5,'E',3 union
    select 6,'F',4 union
    select 7,'G',5 
    go--创建函数
    create function f_nodes(@ID int)
    returns varchar(8000)
    as
    begin
          declare @tb table(ID int,[上级ID] int)
          insert @tb
          select ID,[上级ID] from A where ID=@ID      while @@rowcount>0
          begin
                insert @tb
                select A.ID
                       ,A.[上级ID]
                from A
                join @tb B on A.[上级ID]=B.ID
                where A.ID not in(select ID from @tb)
          end      declare @str varchar(8000)
          set @str=''
          select @str=@str+','+convert(varchar,[ID]) from @tb
          return stuff(@str,1,1,'')
    end
    go-- 查询示例
    select dbo.f_nodes(1) '1的下级'
    select dbo.f_nodes(2) '2的下级'
    select dbo.f_nodes(3) '3的下级'--删除测试环境
    drop function f_nodes
    drop table A--结果
    /*
    1的下级            
    -----------------
    1,2,3,4,5,6,7(所影响的行数为 1 行)2的下级                 
    -----------------------
    2,4,6(所影响的行数为 1 行)3的下级                                       
    ---------------------
    3,5,7(所影响的行数为 1 行)
    */
      

  2.   

    递归函数在小于32层时比较好,如果嵌套层数大于32层,可以用循环:--创建处理函数,查询指定id的所有子结点,同时包含传入的id节点。楼主可适当修改,以供自己需要。
    create function f_id(@id int)
    returns @re table(NodeID int,level int)
    as
    begin
    declare @l int
    set @l=0
    insert @re select @id,@l
    while @@rowcount>0
    begin
    set @l=@l+1
    insert @re select a.NodeID,@l from Tree a join @re b on a.ParentNodeID=b.NodeID  where b.level=@l-1
    end
    return
    end
      

  3.   

    我也知道递归的解法,但是mvm说了不能用递归,也不可以用临时表,那还有什么办法呢?用函数递归处理好一点!create function bom1 (@id int)
    returns @tb table (dirid int,dir nvarchar(10),dirparentid int)
    as
    begin
    insert @tb select dirid,dir,dirparentid from Dir where dirid = @id
    while @@rowcount > 0
    insert @tb select dirid,dir,dirparentid from Dir 
    where dirparentid in (select dirid from @tb)
    and dirid not in (select dirid from @tb)
    return
    end其它办法,看楼下的了!
      

  4.   

    题目说table variable也不能用^_^
      

  5.   

    表变量类似临时表,声明时在tempdb中创建名称形似“#70DF5A86”的临时表,临时表创建时在tempdb中创建名称形似“#tb_________________________________________________________________________________________________________________000000000152“的临时表。只不过表变量的生命期比临时表长
      

  6.   

    http://www.52life.cn/jyk/archive/2005/09/20/261.life这里有
    绝对不会让你失望的。
      

  7.   

    我觉得没有必要用什么递归啊,效率低,你可以改变一下表结构如下:ID  姓名  所属关系路径
    01   A       01/         (01是最上层)
    02   B       01/02       (02是01的下一层)
    03   C       01/02/03     (03是02的下一层)
    04   D       01/02/04     (04是02的下一层)
    ..................................
    如果要找某节点下的所有数据,可以用      select   *   from table   where 所属关系路径   like  '***%'   ****  是那个节点的  所属关系路径 值