sql 多行转成一行

来源:百度知道 编辑:UC知道 时间:2024/07/08 01:31:59
例如表A
id data
1 A
1 B
1 C
2 D
2 F

转换成表B
1 A+B+C
2 D+E
中间用+号或其他符号连接 例中同id数据有2行,3行,实际中不确定几行 求语句
赫赫,就是我不会写才求助的,帮个忙啦
'smerg' 不是可以识别的 函数名。是哪里错了?

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

你的数据结构不合理....