I would like to retrieve the a list of column names in one single string.
Every column name should appear as separated by a comma
Example :
col1, col2, col3
I would like to retrieve the a list of column names in one single string.
Every column name should appear as separated by a comma
Example :
col1, col2, col3
Try like this
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
Or:
DECLARE @Value VARCHAR(8000)
SELECT @Value = COALESCE(@Names + ', ', '') + ColValue FROM People
SELECT @Value
In ORACLE you can do this using the LISTAGG
function:
SELECT
LISTAGG(COLUMN_NAME, ', ')
WITHIN GROUP (ORDER BY COLUMN_NAME) "col names"
FROM USER_TAB_COLUMNS
where TABLE_NAME='Table names here';
Was i to late ? kkk :)