http://community.csdn.net/Expert/topic/4856/4856414.xml?temp=.2089197
参考邹建的帖子,不过在sql2005 调试的。

解决方案 »

  1.   

    create table tb(Kind int,Content varchar(5))
    insert into tb
    select 1,'a' union all
    select 1,'b' union all
    select 2,'c' union all
    select 3,'d' union all
    select 3,'e'
    gocreate function usf_GetContent(@Kind int)
    returns varchar(50)
    as
    begin
    declare @ret varchar(50)
    set @ret=''
    select @ret=@ret+Content+','
    from tb
    where Kind=@Kind
    set @ret=left(@ret,len(@ret)-1)
    return @ret
    end
    goselect Kind,Content=dbo.usf_GetContent(Kind)
    from tbdrop table tb
    drop function usf_GetContentKind        Content                                            
    ----------- -------------------------------------------------- 
    1           a,b
    1           a,b
    2           c
    3           d,e
    3           d,e(所影响的行数为 5 行)
      

  2.   

    修正:应该加上group by
    select Kind,Content=dbo.usf_GetContent(Kind)
    from tb
    group by KindKind        Content                                            
    ----------- -------------------------------------------------- 
    1           a,b
    2           c
    3           d,e(所影响的行数为 3 行)
      

  3.   

    有个问题,我用上面的方法加成功了,但是我想在字符串间不用,号用换行符char(13)。但是出来的数据导到Excel中还是像没有加换行符一样。怎么样解决?
      

  4.   

    char(13) 是回车吧? 
    你用 char(10) 来测试