sql 多行转成一行
例如表A
id data
1 A
1 B
1 C
2 D
2 F
转换成表B
1 A+B+C
2 D+E
smerg是自定义函数
创建一个函数smerg:
create function smerg(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+','+name from 表A where id=@id
set @str=right(@str,len(@str)-1)
return(@str)
End
go
--调用自定义函数得到结果
select distinct id,smerg(id) as name into 表B from 表A
select distinct id,dbo.smerg(id) as name into 表B from 表A