函数里不能用EXECUTE吧
改用过程来做

解决方案 »

  1.   

    打错了。应该说函数不支持动态SQL语句
      

  2.   

    哦,那么我
    --select @classid = b.classid , @re_str = ltrim(b.parentid) + ',' + @re_str from s a , s b where a.classid = @classid and a.parentid = b.classid
     这句中的表名如果要变为参数,整个怎么写呢?
      

  3.   


    函数里面不允许用exec()执行动态的sql语句改用存储过程吧,存储过程也有返回的参数
      

  4.   

    create proc [dbo].[p_getParentPath]
    @classid int output,
    @tbTable nvarchar(20) 
    as
    begin
      declare @sql nvarchar(4000)
      declare @re_str as varchar(100)
      set @re_str = ''
      select @re_str = ltrim(parentid) from s where classid = @classid
      while exists (select 1 from s where classid = @classid and parentid <> 0)
        begin    
         
       --如果直接写表名,没有问题
       set @sql=' select '+@classid+' = b.classid , '+@re_str+'= ltrim(b.parentid) + '','' + '+@re_str+' from '+@tbTable+' a , '+@tbTable+' b where a.classid ='+@classid+' and a.parentid = b.classid'
    exec (@sql)    
       end
    end
     
      

  5.   

    要求:我写了一个自定义函数,如果在函数里面指定表名就不会出错,现在关键,我那里不是表,是一条复合的SQL语句作为查询对像,所以我只能用参数@sql_string传过来.
    请高手指教 create FUNCTION Get_test(@Fenhang NVARCHAR(100),@Sql_string text)
     RETURNS NVARCHAR(1000)
     as 
     begin
      declare @StrRouGuid nvarchar(1000)
      set @StrRouGuid=''
      select @StrRouGuid=@StrRouGuid+','+rowguid_sale from sp_execsql(@Sql_string) where Fenhang=@Fenhang
      set @StrRouGuid=stuff(@StrRouGuid,1,1,'') 
      return @StrRouGuid
     end
    调用select szsh.dbo.Get_test('万科','select rowguid_sale from where 1=2')
    为什么提示 对象名 'sp_execsql' 无效。
      

  6.   

    要求:我写了一个自定义函数,如果在函数里面指定表名就不会出错,现在关键,我那里不是表,是一条复合的SQL语句作为查询对像,所以我只能用参数@sql_string传过来.
    请高手指教create FUNCTION Get_test(@Fenhang NVARCHAR(100),@Sql_string text)
    RETURNS NVARCHAR(1000)
    as 
    begin
    declare @StrRouGuid nvarchar(1000)
    set @StrRouGuid=''
    select @StrRouGuid=@StrRouGuid+','+rowguid_sale from sp_execsql(@Sql_string) where Fenhang=@Fenhang
    set @StrRouGuid=stuff(@StrRouGuid,1,1,'') 
    return @StrRouGuid
    end
    调用select szsh.dbo.Get_test('万科','select rowguid_sale from tbl where 1=2')
    为什么提示 对象名 'sp_execsql' 无效。