使用临时表合并字符串

create table #table(i char(10),n varchar(50))
insert #table select '1','a'
union select '1','b'
union select '1','c'
union select '1','d'
union select '1','e'
union select '1','f'

declare @str varchar(50)
set @str='' /*注意这里*/
select * from #table
update #table
set @str=(case when i='1' then @str+'a'
else n end),i='1',n=@str
select max(n) from #table

drop table #table

其中需要注意的是,如果没有set @str='',则输出结果为null。还不知道为什么原因。
原文地址:https://www.cnblogs.com/ice5/p/1188471.html