--例子
--该问题,写一个合并函数,后,分组合并既可!--测试数据
create Table 表(編號 varchar(20),內容 varchar(20))
insert 表 select 'A','abc'
union all select 'A','aaa'
union all select 'A','dddd'
union all select 'B','1223'
union all select 'B','fkdjfd'
--处理分组合并函数学
Create Function JoinStr(@SNO as varchar(20))
returns varchar(200)
begin
declare @s as varchar(8000)
set @s=''
select @s=@s+','+ltrim(rtrim(內容)) from
(
select 內容 from 表 where 編號=@SNO
)
A
set @s=stuff(@s,1,1,'')
return  @s
end--查询语句
select 編號,dbo.JoinStr(編號) as 内容 from 表 group by 編號--测试结果:
編號     内容
A abc,aaa,dddd
B 1223,fkdjfd
--参考:
http://blog.csdn.net/zlp321002/archive/2005/01/19/260184.aspx

解决方案 »

  1.   

    --测试数据 借用上面的数据
    create Table 表(編號 varchar(20),內容 varchar(20))
    insert 表 select 'A','abc'
    union all select 'A','aaa'
    union all select 'A','dddd'
    union all select 'B','1223'
    union all select 'B','fkdjfd'go
    --处理分组合并函数学create  function f_str(@id varchar(20))returns varchar(8000)
    as
    begin
        declare @ret varchar(8000)
        set @ret=''
        select @ret=@ret+','+value from 表 where id=@id
        return  stuff(@ret,1,1,'')
       
    end
    goselect id,dbo.f_str(id) as name from 表 group by id