如果存在如下数据表:
A B
pro1 qqqqq
pro1 wwwww
pro1 eeeee
pro2 rrrrr
pro2 ttttt
pro1 yyyyy
pro1 uuuuu要实现如下输出:
A B
pro1 qqqqq,wwwww,eeeee,yyyyy,uuuuu
pro2 rrrrr,ttttt
A B
pro1 qqqqq
pro1 wwwww
pro1 eeeee
pro2 rrrrr
pro2 ttttt
pro1 yyyyy
pro1 uuuuu要实现如下输出:
A B
pro1 qqqqq,wwwww,eeeee,yyyyy,uuuuu
pro2 rrrrr,ttttt
returns varchar(8000)
as
begin
declare @s varchar(8000)
select @s = isnull(@s, '') + ',' + B
from 表
where A = @id
set @s = stuff(@s, 1, 1, '')
return @s
end
select A, dbo.fn_cmb(A) from 表
group by A