--代码-------------------------------------------GO GO if object_id(N'dbo.merge',N'FN') is not null drop function dbo.merge GO --函数功能:字符串聚合及去除重复值 create function dbo.merge ( @test_id varchar(50) ) returns varchar(50) as begin --字符串聚合-------------------------START declare @s varchar(8000) set @s = ''
select @s = @s + test_value + ',' from test_a where test_id = @test_id --字符串聚合-------------------------END
insert into test_a select 'a','01,03,04' union all select 'a','02,04' union all select 'b','03,04,08' union all select 'b','06,08,09' union all select 'c','09' union all select 'c','10'
select test_id, test_value = dbo.merge(test_id) from test_a group by test_id