1005

I want to query the name of all columns of a table. I found how to do this in:

But I also need to know: how can this be done in Microsoft SQL Server (2008 in my case)?

sajadre
  • 1,141
  • 2
  • 15
  • 30
odiseh
  • 25,407
  • 33
  • 108
  • 151
  • 65
    As a quick&dirty trick, I really like to do `SELECT * FROM my_table WHERE 1=0` – bgusach Feb 15 '16 at 12:29
  • 27
    @bgusach - Seems like the user wanted the names of columns as rows in a table, but for what you're trying to do, `SELECT TOP 0 * FROM my_table` is less keystrokes – Jake Wood Feb 02 '17 at 17:07
  • 1
    `SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table_name'`; only change the table_name – Ahmad hassan May 03 '23 at 13:12

23 Answers23

1228

You can obtain this information and much, much more by querying the Information Schema views.

This sample query:

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'Customers'

Can be made over all these DB objects:

Luke Burns
  • 1,911
  • 3
  • 24
  • 30
  • 48
    what does "N" in "= N'Customers'" stands for ? – Qbik Jun 10 '14 at 09:11
  • 32
    Qbik "N" if for handlling unicode string like varchar in ANSI(32bit) and nvarchar in unicode(64bit) – thatsalok Jun 25 '14 at 12:36
  • 9
    confirmed: works for **MariaDB** too ! :) (without `Nortwind.` ... ) – jave.web Mar 31 '16 at 09:46
  • 25
    ```SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'tableName';``` this works for me – Pavol Travnik Jan 18 '19 at 15:13
  • 2
    I had to use `TABLE_SCHEMA = '?' AND TABLE_NAME = '?'` since I'm on localhost and I have multiple tables with the same name but in different databases. – akinuri Oct 25 '19 at 11:12
  • 2
    If you don't get the exact table name use TABLE_NAME like 'Customers' instead of TABLE_NAME = N'Customers' – vba Dec 19 '19 at 10:08
  • This throws errors for me in Microsoft SQL Server, but `SELECT name FROM sys.columns WHERE object_id=OBJECT_ID('table_name')` worked. – spareTimeCoder Apr 12 '23 at 12:11
  • This doesn't give you columns for materialized views. – Grasper May 04 '23 at 15:01
  • How can we do it with a temporal table? If I try SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'#Mytemporaltable' it doesn't give any output. – skan Jun 19 '23 at 10:49
218

You can use the stored procedure sp_columns which would return information pertaining to all columns for a given table. More info can be found here http://msdn.microsoft.com/en-us/library/ms176077.aspx

You can also do it by a SQL query. Some thing like this should help:

SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('dbo.yourTableName') 

Or a variation would be:

SELECT   o.Name, c.Name
FROM     sys.columns c 
         JOIN sys.objects o ON o.object_id = c.object_id 
WHERE    o.type = 'U' 
ORDER BY o.Name, c.Name

This gets all columns from all tables, ordered by table name and then on column name.

doğukan
  • 23,073
  • 13
  • 57
  • 69
Arnkrishn
  • 29,828
  • 40
  • 114
  • 128
180
select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='tableName'

This is better than getting from sys.columns because it shows DATA_TYPE directly.

harshit
  • 3,788
  • 3
  • 31
  • 54
Limin
  • 1,847
  • 1
  • 11
  • 3
  • 7
    +1 since this is standard ANSI SQL (https://en.wikipedia.org/wiki/Information_schema) Other answers like sys.objects are not standard – Reversed Engineer Nov 16 '16 at 09:10
  • 1
    And if the table is in another schema (the SQL server "schema" variant) add `AND TABLE_SCHEMA = 'schemaName'` in the `WHERE` clause. – Johan Dec 21 '16 at 19:34
  • 1
    Very useful, and you can add `JOIN sys.types t on c.system_type_id = t.system_type_id` and add `t.name` in your 'SELECT' statement to get the **types** next to each column name as well. – Pac0 Nov 03 '17 at 13:50
63

You can use sp_help in SQL Server 2008.

sp_help <table_name>;

Keyboard shortcut for the above command: select table name (i.e highlight it) and press ALT+F1.

doğukan
  • 23,073
  • 13
  • 57
  • 69
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155
  • 3
    this is my all time favorite keyboard short-cut. I also assign sp_helptext to Cntl-F1. Together these two shortcuts save so much time! – Paul Wehland Sep 21 '18 at 10:57
52

You can write this query to get column name and all details without using INFORMATION_SCHEMA in MySql :

SHOW COLUMNS FROM database_Name.table_name;
Sachin Parse
  • 1,269
  • 11
  • 12
  • 12
    @Benjamin, because this question is for SQL Server and this answer is for MySql – Caimen Nov 29 '16 at 13:47
  • 2
    May be most of the peoples using MySql, faces this problem. And I have mentioned it. I am using MySql. – Sachin Parse Jul 20 '17 at 06:01
  • 10
    It doesn't matter if most people using other RDBMSes have the same problem, it's irrelevant to the original question and pushes relevant answers further down. – Demonblack Oct 04 '18 at 13:57
  • 5
    I downvote because the question is specifically address to mssql – Lucas Dec 20 '19 at 04:04
51

By using this query you get the answer:

select Column_name 
from Information_schema.columns 
where Table_name like 'table name'
Fabien TheSolution
  • 5,055
  • 1
  • 18
  • 30
KuldipMCA
  • 3,079
  • 7
  • 28
  • 48
31
SELECT name
FROM sys.columns
WHERE object_id = OBJECT_ID('TABLE_NAME')

TABLE_NAME is your table

doğukan
  • 23,073
  • 13
  • 57
  • 69
bstricks
  • 823
  • 8
  • 14
29
--This is another variation used to document a large database for conversion (Edited to --remove static columns)

SELECT o.Name                   as Table_Name
     , c.Name                   as Field_Name
     , t.Name                   as Data_Type
     , t.length                 as Length_Size
     , t.prec                   as Precision_
FROM syscolumns c 
     INNER JOIN sysobjects o ON o.id = c.id
     LEFT JOIN  systypes t on t.xtype = c.xtype  
WHERE o.type = 'U' 
ORDER BY o.Name, c.Name

--In the left join, c.type is replaced by c.xtype to get varchar types
doğukan
  • 23,073
  • 13
  • 57
  • 69
Doc
  • 291
  • 3
  • 2
20

You can try this.This gives all the column names with their respective data types.

desc <TABLE NAME> ;
ishaan arora
  • 523
  • 8
  • 18
18
SELECT column_name, data_type, character_maximum_length, table_name,ordinal_position, is_nullable 
FROM information_schema.COLUMNS WHERE table_name LIKE 'YOUR_TABLE_NAME'
ORDER BY ordinal_position
Taryn
  • 242,637
  • 56
  • 362
  • 405
Petko Petkov
  • 181
  • 1
  • 2
16

Just run this command

EXEC sp_columns 'Your Table Name'
Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82
Hardeep Singh
  • 818
  • 9
  • 14
16

Summarizing the Answers

I can see many different answers and ways to do this but there is the rub in this and that is the objective.

Yes, the objective. If you want to only know the column names you can use

SELECT * FROM my_table WHERE 1=0
or
SELECT TOP 0 * FROM my_table

But if you want to use those columns somewhere or simply say manipulate them then the quick queries above are not going to be of any use. You need to use

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'Customers'

one more way to know some specific columns where we are in need of some similar columns

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME like N'%[ColumnName]%' and TABLE_NAME = N'[TableName]'
Shreekant
  • 429
  • 3
  • 16
16

You can try using :-

USE db_name;
DESCRIBE table_name;

it'll give you column names with the type.

officialrahulmandal
  • 2,473
  • 1
  • 23
  • 31
13

In SQL Server, you can select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS.

Here is the code:

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='YourTableName'
MB_18
  • 1,620
  • 23
  • 37
12

This SO question is missing the following approach :

-- List down all columns of table 'Logging'
select * from sys.all_columns where object_id = OBJECT_ID('Logging')
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
11

It will check whether the given the table is Base Table.

SELECT 
    T.TABLE_NAME AS 'TABLE NAME',
    C.COLUMN_NAME AS 'COLUMN NAME'
FROM INFORMATION_SCHEMA.TABLES T
INNER JOIN INFORMATION_SCHEMA.COLUMNS C ON T.TABLE_NAME=C.TABLE_NAME
    WHERE   T.TABLE_TYPE='BASE TABLE'
            AND T.TABLE_NAME LIKE 'Your Table Name'
Prahalad Gaggar
  • 11,389
  • 16
  • 53
  • 71
7

you can use this query

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME like N'%[ColumnName]%' and TABLE_NAME = N'[TableName]'
R.Akhlaghi
  • 729
  • 1
  • 12
  • 23
6
SELECT c.Name 
FROM sys.columns c
JOIN sys.objects o ON o.object_id = c.object_id
WHERE o.object_id = OBJECT_ID('TABLE_NAME')
ORDER BY c.Name
k06a
  • 17,755
  • 10
  • 70
  • 110
5

One other option which is arguably more intuitive is:

SELECT [name] 
FROM sys.columns 
WHERE object_id = OBJECT_ID('[yourSchemaType].[yourTableName]') 

This gives you all your column names in a single column. If you care about other metadata, you can change edit the SELECT STATEMENT TO SELECT *.

Samuel Nde
  • 2,565
  • 2
  • 23
  • 23
3
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME like N'%[ColumnName]%' and TABLE_NAME = N'[TableName]'

OR If you want to view the column names, you can select the table and press Alt+F1.

Alireza Roshanzamir
  • 1,165
  • 6
  • 17
  • This answer is not adding anything not provided in earlier answers, such as [this](https://stackoverflow.com/a/38116877) that was posted 7 years before yours. – Thom A Jul 18 '23 at 12:57
2

Simple and doesn't require sys variables:

SHOW COLUMNS FROM suppliers;
1
SELECT TOP (0) [toID]
      ,[sourceID]
      ,[name]
      ,[address]
  FROM [ReportDatabase].[Ticket].[To]

Simple and doesnt require any sys tables

Hawkzey
  • 1,088
  • 1
  • 11
  • 21
0

Some SQL Generating SQL:

DROP TABLE IF EXISTS test;
CREATE TABLE test (
  col001 INTEGER
, col002 INTEGER
, col003 INTEGER
, col004 INTEGER
, col005 INTEGER
, col006 INTEGER
, col007 INTEGER
, col008 INTEGER
, col009 INTEGER
, col010 INTEGER
)
;
INSERT INTO test(col001) VALUES(1);
INSERT INTO test(col002) VALUES(1);
INSERT INTO test(col005) VALUES(1);
INSERT INTO test(col009) VALUES(1);
INSERT INTO test VALUES (NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);

SELECT
  CASE ROW_NUMBER() OVER(ORDER BY ordinal_position)
  WHEN 1 THEN 
    'SELECT'+CHAR(10)+'  *'+CHAR(10)+'FROM test'
   +CHAR(10)+'WHERE '
  ELSE
    '   OR '
  END
+ column_name +' IS NOT NULL'
+ CASE ROW_NUMBER() OVER(ORDER BY ordinal_position DESC)
  WHEN 1 THEN 
    CHAR(10)+';'
  ELSE
    ''
  END
  FROM information_schema.columns
  WHERE table_schema='dbo'
    AND table_name = 'test'
ORDER BY
  ordinal_position;

-- the whole scenario. Works for 10 , will work for 100, too:

-- out -----------------------------------------------
-- out  SELECT
-- out   *
-- out FROM test
-- out WHERE col001 IS NOT NULL
-- out     OR col002 IS NOT NULL
-- out     OR col003 IS NOT NULL
-- out     OR col004 IS NOT NULL
-- out     OR col005 IS NOT NULL
-- out     OR col006 IS NOT NULL
-- out     OR col007 IS NOT NULL
-- out     OR col008 IS NOT NULL
-- out     OR col009 IS NOT NULL
-- out     OR col010 IS NOT NULL
-- out ;

marcothesane
  • 6,192
  • 1
  • 11
  • 21