create table 表
(id int, name varchar(10))
insert 表
select 1 ,'aaa'
union all
select 1 ,'bbb'
union all
select 2 ,'ccc'
union all
select 3 ,'ddd'
union all
select 3 ,'ccc'
create function cx(@id int)
returns varchar(10)
as
begin
declare @r varchar(10)
set @r=''
select @r=@r+','+name from 表 where @id=id
set @r=stuff(@r,1,1,'')
return @r
end
select id,dbo.cx(id)name from 表
group by id
id name
----------- ----------
1 aaa,bbb
2 ccc
3 ddd,ccc