读书人

合拢多列

发布时间: 2013-04-02 12:35:26 作者: rapoo

合并多列


[解决办法]
select distinct id,
(select ''+ch from test where id=a.id for xml path(''))
from test a

[解决办法]

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

读书人网 >SQL Server

热点推荐