I would like to get a list of column headers for a table.
I have queried
SELECT TOP 0 * FROM TABLE_NAME.
But now I want to pivot those column names into rows. How do I do that?
Using the Stack Overflow database as an example:
SELECT TOP (0) * FROM dbo.Users;
Id | AboutMe | Age | CreationDate | DisplayName | DownVotes | EmailHash | LastAccessDate | Location | Reputation | UpVotes | Views | WebsiteUrl | AccountId |
---|
The display you want is available directly from system metadata without pivoting:
SELECT
C.[name] -- returns the column name
FROM sys.columns AS C
WHERE
-- Put your table name inside the OBJECT_ID function here
-- The 'U' parameter ensures we only match user tables
C.[object_id] = OBJECT_ID(N'dbo.Users', 'U')
-- Don't show any hidden columns
AND C.is_hidden = 'false'
ORDER BY
-- Return columns in left-to-right order
C.column_id;
name |
---|
Id |
AboutMe |
Age |
CreationDate |
DisplayName |
DownVotes |
EmailHash |
LastAccessDate |
Location |
Reputation |
UpVotes |
Views |
WebsiteUrl |
AccountId |