以下的函数我是在网上找到的,我现在想改成通用的,加了个参数@tablename 但加了后就说tablename 没有定义。应该怎么改?
还有下面这句是什么意思?insert @re select @id,@l while @@rowcount>0set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
/*--树形数据处理   
  查询指定id的所有
  --调用示例   
  --调用(查询所有的子)   
  select   a.*,层次=b.[level]   from   tb_name   a,f_cid(2)b   where   a.sid=b.id   
  --*/   
Create function f_nid(@id int,@tablename varchar(50)=NULL)
returns @re table(id varchar(20),[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.id,@l from @tablename  a,@re b where a.sid=b.id and b.[level]=@l-1
end
return
end
GO

解决方案 »

  1.   


    /*
    标题:查询指定节点及其所有子节点的函数
    作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2008-05-12
    地点:广东深圳
    */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@@ROWCOUNT:返回受上一语句影响的行数。
    返回类型:integer。
    注释:任何不返回行的语句将这一变量设置为 0 ,如 IF 语句。
    示例:下面的示例执行 UPDATE 语句并用 @@ROWCOUNT 来检测是否有发生更改的行。UPDATE authors SET au_lname = 'Jones' WHERE au_id = '999-888-7777'
    IF @@ROWCOUNT = 0
       print 'Warning: No rows were updated'结果:(所影响的行数为 0 行)
    Warning: No rows were updated
      

  2.   

    --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
    ----------------------------------------------------------------------------------------------------------------
      

  3.   

    通用的?
    表名不确定要使用动态SQL.
    函数中不能使用动态SQL.
    看看能否用存储过程来完成.
      

  4.   

    函数中不能用动态sql
    只能用存储过程实现.
      

  5.   

    表名是个变量,应该用动态SQL