How can I get column names from a table in SQL Ser

2020-01-22 12:28发布

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

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

19条回答
淡お忘
2楼-- · 2020-01-22 12:36

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')
查看更多
何必那么认真
3楼-- · 2020-01-22 12:37

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

查看更多
劳资没心,怎么记你
4楼-- · 2020-01-22 12:38

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.

查看更多
Melony?
5楼-- · 2020-01-22 12:38
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'name_of_your_table'
查看更多
Animai°情兽
6楼-- · 2020-01-22 12:39

By using this query you get the answer:

select Column_name 
from Information_schema.columns 
where Table_name like 'table name'
查看更多
Viruses.
7楼-- · 2020-01-22 12:41
SELECT name
FROM sys.columns
WHERE object_id = OBJECT_ID('TABLE_NAME')

TABLE_NAME is your table

查看更多
登录 后发表回答