For the necessity of my application, I must return the column names of a query as the very first row. Now I must PIVOT this result in order to UNION it with my result set, but the difficult part is: it must be dynamic, so if I ever add new columns to this table, the SELECT will bring all the names pivoted.
The following SELECT brings me the Column names:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Codes'
ORDER BY INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION
And my result set is:
COLUMN_NAME
Id
CodeName
Country
StartDate
EndDate
What I expect is:
Id CodeName Country StartDate EndDate (... whatever other columns I might have)
Is there any easy way to do that without hardcoding the column names?
Thank you in advance!