1,216
社区成员
发帖
与我相关
我的任务
分享
create table tb(id int,name varchar(100))
insert into tb(id,name) values (1,'a')
insert into tb(id,name) values (1,'b')
insert into tb(id,name) values (1,'c')
insert into tb(id,name) values (2,'d')
insert into tb(id,name) values (2,'e')
insert into tb(id,name) values (2,'f')
create function getStr(@id as int)
returns varchar(4000)
as
begin
declare @s as varchar(4000)
set @s=''
select @s=@s+ ',' + name from tb where id=@id
return stuff(@s,1,1,'')
end
select id,dbo.getStr(id) as name from tb group by id