create function f (no int)
returns varchar(4000)
as
begin
declare @ret varchar(4000)
set @ret=''
select @ret=@ret+str from t1
return @ret
end

解决方案 »

  1.   

    create function f(@No int)
    returns varchar(8000)
    as
    begin
    declare @tmp varchar(8000)
    set @tmp=''
    select @tmp=@tmp+str from t where no=@no
    return @tmpend
    goselect no, f(no) from t group by no
      

  2.   

    create function dbo.f(@No int)
    returns varchar(1000)
    as 
    begin
    declare @s varchar(1000)
    set @s=''
    select @s=@s+''+col from tb where no=@no
    return(stuff(@s,1,1,' '))
    endgo select no,dbo.f(no) from tb group by no
      

  3.   

    如果同一个 no ,最多只有两个 str ,连接可以这样写:
    declare @t table(id int, no int, str varchar(10))
    insert @t
    select 1,   1,   'aa' union all
    select 2,   2 ,  'bb' union all
    select 3,  1,   'cc'select no,sdf=case when count(*)=2 then min([str])+max([str]) else min([str]) end 
    from @t group by [no]当分组中子项最多有三个情况,都可以直接用一句SQL语句实现要求
    超过三个,就要用函数了
      

  4.   

    你只要做一个返回类型为“表”的函数就可以了吧。
    create funcation fun ()
    RETURNS @T TABLE(no int,f varchar(100))
    as 
    insert into @T (select no, f(str) from t group by no)
    return
      

  5.   


    select no, f(str) from t group by no这条查询语句还是不能实现阿,郁闷
      

  6.   

    select no,dbo.f(no) as str from t group by no
      

  7.   

    create function f (no int)
    returns varchar(4000)
    as
    begin
    declare @ret varchar(4000)
    set @ret=''
    select @ret=@ret+str from t1
    return @ret
    end
    没有问题!!
      

  8.   

    我觉得itblog的create function dbo.f(@No int)
    returns varchar(1000)
    as 
    begin
    declare @s varchar(1000)
    set @s=''
    select @s=@s+''+col from tb where no=@no
    return(stuff(@s,1,1,' '))
    endgo select no,dbo.f(no) from tb group by no
    是可行的