我想在一个字符串中检索列名列表。
每个列名称应以逗号
分隔示例:
col1, col2, col3
答案 0 :(得分:2)
试试这个
Select Stuff(
(
Select ', ' + T2.ColVlaue // Add a comma (,) before each value
From MyTable As T2
Where T2.ID= T1.ID
For Xml Path(''), type // Select it as XML
).value('.', 'nvarchar(max)'), 1, 2, '') // This is done to remove the first character (,) from the result
From MyTable As T1
Group By T1.Id
或者:
DECLARE @Value VARCHAR(8000)
SELECT @Value = COALESCE(@Names + ', ', '') + ColValue FROM People
SELECT @Value
答案 1 :(得分:0)
在ORACLE中,您可以使用LISTAGG
功能执行此操作:
SELECT
LISTAGG(COLUMN_NAME, ', ')
WITHIN GROUP (ORDER BY COLUMN_NAME) "col names"
FROM USER_TAB_COLUMNS
where TABLE_NAME='Table names here';
我迟到了吗? kkk:)