create table aa(c1 char(2),c2 varchar(10))
insert into aa values( '11','aaa')
insert into aa values('11','aab')
insert into aa values('21','ccc')
insert into aa values('11','aac')
如何实现结果:
c1 c2
11 aaa,aab,aac
21 ccc
---------------------------------------------------------
insert into aa values( '11','aaa')
insert into aa values('11','aab')
insert into aa values('21','ccc')
insert into aa values('11','aac')
如何实现结果:
c1 c2
11 aaa,aab,aac
21 ccc
---------------------------------------------------------
实现方法
创建一个合并函数
create function getstr(@content varchar(10))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+','+rtrim(c2) from aa where c1=@content
select @str=right(@str,len(@str)-1)
return @str
end
go
--调用:
select c1,dbo.getstr(c1) c2 from aa group by c1
create function getstr(@content varchar(10))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+','+rtrim(c2) from aa where c1=@content
select @str=right(@str,len(@str)-1)
return @str
end
go
--调用:
select c1,dbo.getstr(c1) c2 from aa group by c1