备注 | 修改日期 | 修改人 |
其他原因... | 2019-05-05 17:39:38[当前版本] | 系统管理员 |
CREAT | 2019-03-12 11:57:02 | 系统管理员 |
转自:https://blog.csdn.net/winnyrain/article/details/77317645
stuff(select ',' + fieldname from tablename for xml
path('')),1,1,'')
这一整句的作用是将多行fieldname字段的内容串联起来,用逗号分隔。
例如表
fieldname
-----------
AAA
BBB
CCC
串联之后就是字符串: AAA,BBB,CCC
for xml path是SQL Server
2005以后版本支持的一种生成XML的方式。具体如何使用,请参考联机丛书。
stuff函数的作用是去掉字符串最前面的逗号分隔符。
用法:
select *,stuff(select ',' + fieldname from table1 for xml path('')),1,1,'') as field from table2