拼接整个集合中的所有行
declare @s varchar(100);
set @s = '';
select @s = @s + ',' + cast(n as varchar(max))
from t;
print @s
结果类似:
,1,2,1,2,1,2,1,2,1,2,1,2
拼接集合中部分行
select distinct t.n_uid, ','+t2.tags as tags
from crm_tagging t
cross apply (
select cast(t1.n_tag as varchar(max)) + ','
from crm_tagging t1
where t1.n_inuse=1 and t.n_uid=t1.n_uid
for xml path('')
) t2 (tags)
where t.n_inuse=1
结果类似:
n_uid tags
60859 ,1,2,
61629 ,1,2,
61693 ,1,2,
61809 ,1,2,