Richard C.
—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?
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|
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.