如何从SQL服务器的表中获取列名?

时间:2022-09-23 21:28:56

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)?

但是我需要知道:在Microsoft SQL Server(我的案例中是2008年),这是怎么做到的?

16 个解决方案

#1


603  

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

通过查询信息模式视图,您可以获得这些信息和更多信息。

This sample query:

这个示例查询:

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

Can be made over all these DB objects:

可以对所有这些DB对象:

#2


154  

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

您可以使用存储过程sp_columns,它将返回关于给定表的所有列的信息。更多的信息可以在这里找到http://msdn.microsoft.com/en-us/library/ms176077.aspx。

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

您也可以通过SQL查询来实现它。像这样的事情应该会有所帮助。

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.

这将从所有表中获取所有列,按表名排序,然后按列名排序。

#3


99  

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

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

这比从sys得到的要好。列是因为它直接显示DATA_TYPE。

#4


43  

You can use sp_help in SQL Server 2008.

您可以在SQL Server 2008中使用sp_help。

sp_help <table_name>;

Keyboard short-cut for the above command select table name (highlight it) and press ALT+F1.

以上命令的键盘快捷键选择表名(高亮显示)并按ALT+F1。

#5


36  

By using this query you get the answer:

通过使用这个查询,您得到了答案:

select Column_name 
from Information_schema.columns 
where Table_name like 'table name'

#6


23  

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

——在左边,c。类型替换为c。xtype获得varchar类型。

#7


18  

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

无需在MySql中使用INFORMATION_SCHEMA,就可以编写此查询以获取列名和所有详细信息:

SHOW COLUMNS FROM database_Name.table_name;

#8


14  

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

#9


14  

SELECT name
FROM sys.columns
WHERE object_id = OBJECT_ID('TABLE_NAME')

// TABLE_NAME is your table

// TABLE_NAME是您的表。

#10


11  

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

你可以试试这个。这将为所有列名称提供它们各自的数据类型。

desc <TABLE NAME> ;

#11


10  

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')

#12


8  

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'

#13


8  

Just run this command

运行这个命令

EXEC sp_columns 'Your Table Name'

EXEC sp_columns 'Your Table Name'

#14


3  

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

#15


2  

you can use this query

您可以使用此查询。

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME like N'%[ColumnName]%' and TABLE_NAME = N'[TableName]'

#16


0  

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'name_of_your_table'

#1


603  

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

通过查询信息模式视图,您可以获得这些信息和更多信息。

This sample query:

这个示例查询:

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

Can be made over all these DB objects:

可以对所有这些DB对象:

#2


154  

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

您可以使用存储过程sp_columns,它将返回关于给定表的所有列的信息。更多的信息可以在这里找到http://msdn.microsoft.com/en-us/library/ms176077.aspx。

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

您也可以通过SQL查询来实现它。像这样的事情应该会有所帮助。

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.

这将从所有表中获取所有列,按表名排序,然后按列名排序。

#3


99  

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

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

这比从sys得到的要好。列是因为它直接显示DATA_TYPE。

#4


43  

You can use sp_help in SQL Server 2008.

您可以在SQL Server 2008中使用sp_help。

sp_help <table_name>;

Keyboard short-cut for the above command select table name (highlight it) and press ALT+F1.

以上命令的键盘快捷键选择表名(高亮显示)并按ALT+F1。

#5


36  

By using this query you get the answer:

通过使用这个查询,您得到了答案:

select Column_name 
from Information_schema.columns 
where Table_name like 'table name'

#6


23  

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

——在左边,c。类型替换为c。xtype获得varchar类型。

#7


18  

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

无需在MySql中使用INFORMATION_SCHEMA,就可以编写此查询以获取列名和所有详细信息:

SHOW COLUMNS FROM database_Name.table_name;

#8


14  

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

#9


14  

SELECT name
FROM sys.columns
WHERE object_id = OBJECT_ID('TABLE_NAME')

// TABLE_NAME is your table

// TABLE_NAME是您的表。

#10


11  

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

你可以试试这个。这将为所有列名称提供它们各自的数据类型。

desc <TABLE NAME> ;

#11


10  

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')

#12


8  

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'

#13


8  

Just run this command

运行这个命令

EXEC sp_columns 'Your Table Name'

EXEC sp_columns 'Your Table Name'

#14


3  

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

#15


2  

you can use this query

您可以使用此查询。

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME like N'%[ColumnName]%' and TABLE_NAME = N'[TableName]'

#16


0  

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'name_of_your_table'