How can I get column names from a table in SQL Server?

Richard C.

The Problem

If you have a table in Microsoft SQL Server with multiple columns, how do you view their names quickly in a query? How do you see detailed information for each column? And how do you select and return them in a query, ideally in a single string?

The Solution

Assume you have a table called Person with a few columns, like the following:

CREATE TABLE Person ( Id INT PRIMARY KEY, Name VARCHAR(255), Address VARCHAR(255), Description VARCHAR(255) );

The fastest way to see the table columns is to run a query returning no rows:

SELECT TOP 0 * FROM Person; -- Id|Name|Address|Description| -- --+----+-------+-----------+

If you want to see detailed information on the columns, in cases when you might want to alter the table, there are two options. They return slightly different columns, with the second option having slightly more information. Pick whichever style you prefer.

Note that the N before a table name is optional. You need it only if your table name contains Unicode characters outside standard Latin letters.

Option 1:

EXEC sp_columns N'Person'; -- TABLE_QUALIFIER|TABLE_OWNER|TABLE_NAME|COLUMN_NAME|DATA_TYPE|TYPE_NAME|PRECISION|LENGTH|SCALE|RADIX|NULLABLE|REMARKS|COLUMN_DEF|SQL_DATA_TYPE|SQL_DATETIME_SUB|CHAR_OCTET_LENGTH|ORDINAL_POSITION|IS_NULLABLE|SS_DATA_TYPE| -- ---------------+-----------+----------+-----------+---------+---------+---------+------+-----+-----+--------+-------+----------+-------------+----------------+-----------------+----------------+-----------+------------+ -- Test |dbo |Person |Id | 4|int | 10| 4| 0| 10| 0| | | 4| | | 1|NO | 56| -- Test |dbo |Person |Name | 12|varchar | 255| 255| | | 1| | | 12| | 255| 2|YES | 39| -- Test |dbo |Person |Address | 12|varchar | 255| 255| | | 1| | | 12| | 255| 3|YES | 39| -- Test |dbo |Person |Description| 12|varchar | 255| 255| | | 1| | | 12| | 255| 4|YES | 39|

Option 2:

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'Person'; -- TABLE_CATALOG|TABLE_SCHEMA|TABLE_NAME|COLUMN_NAME|ORDINAL_POSITION|COLUMN_DEFAULT|IS_NULLABLE|DATA_TYPE|CHARACTER_MAXIMUM_LENGTH|CHARACTER_OCTET_LENGTH|NUMERIC_PRECISION|NUMERIC_PRECISION_RADIX|NUMERIC_SCALE|DATETIME_PRECISION|CHARACTER_SET_CATALOG|CHARACTER_SET_SCHEMA|CHARACTER_SET_NAME|COLLATION_CATALOG|COLLATION_SCHEMA|COLLATION_NAME |DOMAIN_CATALOG|DOMAIN_SCHEMA|DOMAIN_NAME| -- -------------+------------+----------+-----------+----------------+--------------+-----------+---------+------------------------+----------------------+-----------------+-----------------------+-------------+------------------+---------------------+--------------------+------------------+-----------------+----------------+----------------------------+--------------+-------------+-----------+ -- Test |dbo |Person |Id | 1| |NO |int | | | 10| 10| 0| | | | | | | | | | | -- Test |dbo |Person |Name | 2| |YES |varchar | 255| 255| | | | | | |iso_1 | | |SQL_Latin1_General_CP1_CI_AS| | | | -- Test |dbo |Person |Address | 3| |YES |varchar | 255| 255| | | | | | |iso_1 | | |SQL_Latin1_General_CP1_CI_AS| | | | -- Test |dbo |Person |Description| 4| |YES |varchar | 255| 255| | | | | | |iso_1 | | |SQL_Latin1_General_CP1_CI_AS| | | |

Finally, if you want to return the column names in one string, in case you want to display them in an app, you can concatenate them with the query below:

SELECT STRING_AGG(COLUMN_NAME, ', ') AS Columns FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Person'; -- Columns | -- ------------------------------+ -- Id, Name, Address, Description|

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

    TwitterGitHubDribbbleLinkedinDiscord
© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.