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

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


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.NameFROM     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.


select *from INFORMATION_SCHEMA.COLUMNSwhere TABLE_NAME='tableName'

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