本文介绍了将单行的n列值连接成逗号分隔的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个表,我正在尝试快速记录存储过程。



我知道如何将表的列变为CSV字符串:

 声明  @ columnList   varchar  8000 
select @columnList = stuff((选择 ' ,' + COLUMN_NAME
来自 INFORMATION_SCHEMA.COLUMNS
其中 TABLE_NAME = @ currentTable
订单 ORDINAL_POSITION
for xml路径(' ')),
1 1 ' '

选择 @ columnList



结果为:

col1,col2,col3,...,colN





但是,现在我想用一行数据做同样的事情。例如:

 ++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + b ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > 



我希望所有值都显示为:

Val1,Val2,Val3,.. 。,ValN



这可能吗?



-Wesley

解决方案




I have multiple tables that I am attempting to log quickly for a stored procedure.

I know how to get the columns for the table into a CSV string:

declare @columnList varchar(8000)
select @columnList=stuff( (select ','+COLUMN_NAME
				from INFORMATION_SCHEMA.COLUMNS
				where TABLE_NAME=@currentTable
				order by ORDINAL_POSITION
				for xml path('')),
				1,1,'')

select @columnList


Resulting as:
col1,col2,col3,...,colN


However, now I want to do the same thing with a single row of data. Example:

+++++++++++++++++++++++++++++++++++++++++++
+UnitID+ Col1 + Col2 + Col3 + ... + Col N +
+++++++++++++++++++++++++++++++++++++++++++
+  ID  + Val1 + Val2 + Val3 + ... + Val N +
+++++++++++++++++++++++++++++++++++++++++++



I want all of the values to show up as:
Val1,Val2,Val3,...,ValN

Is this possible?

-Wesley

解决方案




这篇关于将单行的n列值连接成逗号分隔的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:46