Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

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

I need to question the name of all columns of a table. I discovered how to do this in:
Oracle
MySQL
PostgreSQL

Yet, I likewise need to know: how might this be done in Microsoft SQL Server?
by

2 Answers

espadacoder11

select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='tableName'

This is better than getting from sys.columns because it shows DATA_TYPE directly.
kshitijrana14
You can use the stored procedure sp_columns which would return information pertaining to all columns for a given table.
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.

Login / Signup to Answer the Question.