0

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?

0

1 Answer 1

3

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.