SQL Server利用Stuff+for xml path 将列值快速的拼接成字符串及创建拼接视图

典型用法:
将查询出的列值转换成字符串赋值给变量
以下代码演示所有部门ID为A300的员工姓名查询出来并用逗号串联成一个字符串,去除开头的逗号,赋值给变量
declare @ss nvarchar(1000)
select
@ss =(
stuff(
(select ‘,’ + Emp_CName from sys_User where Dept_ID = ‘A300’ for xml path(’’)),
1,
1,
‘’
)
)
select @ss
拼接结果:唐xx,林xx,张xx
参考博文:https://www.cnblogs.com/stevenjson/p/3673239.html

猜你喜欢

转载自blog.csdn.net/qq_33788547/article/details/83828868