合并多列
if OBJECT_ID('aaa') is not null
drop table aaa
if OBJECT_ID('combin') is not null
drop function combin
go
Create table aaa(ID int,ch char(1))
insert into aaa values(1,'a')
insert into aaa values(1,'b')
insert into aaa values(2,'x')
insert into aaa values(2,'y')
insert into aaa values(2,'z')
go
create function combin(@id int)
returns varchar(10)
as begin
declare @val varchar(10)=''
select @val=@val+ch from aaa where id=@id
return @val
end
go
select id,dbo.combin(id) from aaa group by ID