sql 字符串合并

时间:2014-12-04 00:42:46   收藏:0   阅读:382
create table tb(id int, value varchar(10)) insert into tb values(1, ‘aa‘) insert into tb values(1, ‘bb‘) insert into tb values(2, ‘aaa‘) insert into tb values(2, ‘bbb‘) insert into tb values(2, ‘ccc‘) go -- 方法一: -- 1. 创建Function create function dbo.f_str(@id varchar(10)) returns varchar(1000) as begin declare @str varchar(1000) select @str = isnull(@str + ‘,‘ , ‘‘) + cast(value as varchar) from tb where id = @id return @str end go -- 2. 调用Function select id , value = dbo.f_str(id) from tb group by id drop function dbo.f_str -- 方法二: select id, [value] = stuff((select ‘,‘ + [value] from tb t where id = tb.id for xml path(‘‘)) , 1 , 1 , ‘‘) from tb group by id drop table tb

原文:http://www.cnblogs.com/zisezhixin/p/4141591.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!