[英]How do you return the column names of a table?
How would I return the column names of a table using SQL Server 2008?如何使用 SQL Server 2008 返回表的列名? ie a table contains these columns- id, name, address, country and I want to return these as data.
即一个表包含这些列 - id、姓名、地址、国家,我想将这些作为数据返回。
Not sure if there is an easier way in 2008 version.不确定 2008 版本中是否有更简单的方法。
USE [Database Name]
SELECT COLUMN_NAME,*
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'YourTableName' AND TABLE_SCHEMA='YourSchemaName'
这是最简单的方法
exec sp_columns [tablename]
像这样的东西?
sp_columns @table_name=your table name
One method is to query syscolumns:一种方法是查询 syscolumns:
select
syscolumns.name as [Column],
syscolumns.xusertype as [Type],
sysobjects.xtype as [Objtype]
from
sysobjects
inner join
syscolumns on sysobjects.id = syscolumns.id
where sysobjects.xtype = 'u'
and sysobjects.name = 'MyTableName'
order by syscolumns.name
This seems a bit easier then the above suggestions because it uses the OBJECT_ID() function to locate the table's id.这似乎比上面的建议容易一些,因为它使用OBJECT_ID()函数来定位表的 id。 Any column with that id is part of the table.
具有该 ID 的任何列都是表的一部分。
SELECT *
FROM syscolumns
WHERE id=OBJECT_ID('YOUR_TABLE')
I commonly use a similar query to see if a column I know is part of a newer version is present.我通常使用类似的查询来查看我知道的列是否存在于较新版本中。 It is the same query with the addition of {AND name='YOUR_COLUMN'} to the where clause.
这是在 where 子句中添加 {AND name='YOUR_COLUMN'} 的相同查询。
IF EXISTS (
SELECT *
FROM syscolumns
WHERE id=OBJECT_ID('YOUR_TABLE')
AND name='YOUR_COLUMN'
)
BEGIN
PRINT 'Column found'
END
try this尝试这个
select * from <tablename> where 1=2
............................................... ......................................................
The following seems to be like the first suggested query above but sometime you have to specify the database to get it to work.以下似乎类似于上面的第一个建议查询,但有时您必须指定数据库才能使其工作。 Note that the query should also work without specifying the TABLE_SCHEMA:
请注意,查询也应该在不指定 TABLE_SCHEMA 的情况下工作:
SELECT COLUMN_NAME
FROM YOUR_DB_NAME.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'YOUR_TABLE_NAME' AND TABLE_SCHEMA = 'YOUR_DB_NAME'
I use我用
SELECT st.NAME, sc.NAME, sc.system_type_id
FROM sys.tables st
INNER JOIN sys.columns sc ON st.object_id = sc.object_id
WHERE st.name LIKE '%Tablename%'
Why not just try this:为什么不试试这个:
right click on the table -> Script Table As -> Create To -> New Query Editor Window?
右键单击表 -> Script Table As -> Create To -> New Query Editor Window?
The entire list of columns are given in the script.脚本中给出了整个列列表。 Copy it and use the fields as necessary.
复制它并根据需要使用这些字段。
USE[Database] SELECT TABLE_NAME,TABLE_SCHEMA,[Column_Name],[Data_type] FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='dbo'
While @Gulzar Nazim's answer is great, it is probably easier to include the database name in the query, which could be achieved by the following SQL.虽然@Gulzar Nazim 的回答很好,但在查询中包含数据库名称可能更容易,这可以通过以下 SQL 实现。
SELECT COLUMN_NAME, *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'you-table-name' AND TABLE_CATALOG='your-database-name'
You can use the below code to print all column names;您可以使用以下代码打印所有列名; You can also modify the code to print other details in whichever format u like
您还可以修改代码以您喜欢的任何格式打印其他详细信息
declare @Result varchar(max)='
'
select @Result=@Result+''+ColumnName+'
'
from
(
select
replace(col.name, ' ', '_') ColumnName,
column_id ColumnId
from sys.columns col
join sys.types typ on
col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
where object_id = object_id('tblPracticeTestSections')
) t
order by ColumnId
print @Result
Output输出
column1
column2
column3
column4
To use the same code to print the table and its column name as C# class use the below code:要使用与 C# 类相同的代码打印表及其列名,请使用以下代码:
declare @TableName sysname = '<EnterTableName>'
declare @Result varchar(max) = 'public class ' + @TableName + '
{'
select @Result = @Result + '
public static string ' + ColumnName + ' { get { return "'+ColumnName+'"; } }
'
from
(
select
replace(col.name, ' ', '_') ColumnName,
column_id ColumnId
from sys.columns col
join sys.types typ on
col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
where object_id = object_id(@TableName)
) t
order by ColumnId
set @Result = @Result + '
}'
print @Result
Output:输出:
public class tblPracticeTestSections
{
public static string column1 { get { return "column1"; } }
public static string column2{ get { return "column2"; } }
public static string column3{ get { return "column3"; } }
public static string column4{ get { return "column4"; } }
}
I'm not sure if the syscolumns.colid value is the same as the 'ORDINAL_POSITION' value returned as part of sp_columns, but in what follows I am using it that way - hope I'm not misinforming...我不确定 syscolumns.colid 值是否与作为 sp_columns 一部分返回的 'ORDINAL_POSITION' 值相同,但在接下来的内容中我是这样使用它的 - 希望我没有误导......
Here's a slight variation on some of the other answers I've found - I use this because the 'position' or order of the column in the table is important in my application - I basically need to know 'What is column (n) called?'这是我发现的其他一些答案的轻微变化 - 我使用它是因为表格中列的“位置”或顺序在我的应用程序中很重要 - 我基本上需要知道“列(n)是什么?'
sp_columns returns a whole bunch of extraneous stuff, and I'm handier with a select than T-SQL functions, so I went this route: sp_columns 返回一大堆无关的东西,我用 select 比 T-SQL 函数更方便,所以我走这条路:
select
syscolumns.name,
syscolumns.colid
from
sysobjects, syscolumns
where
sysobjects.id = syscolumns.id and
sysobjects.xtype = 'u' and
sysobjects.name = '<YOUR_TABLE>'
order by syscolumns.colid
CREATE PROCEDURE [dbo].[Usp_GetColumnName]
@TableName varchar(50)
AS
BEGIN
BEGIN
SET NOCOUNT ON
IF (@TableName IS NOT NULL)
select ORDINAL_POSITION OrderPosition,COLUMN_NAME ColumnName from information_schema.columns
where table_name =@TableName
order by ORDINAL_POSITION
END
END
I just use a query like Martin Smith mentioned, just little shorter:我只是使用像 Martin Smith 提到的查询,只是短一点:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'YourTableName'
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'TableName'
Since SysColumns is deprecated , use Sys.All_Columns
:由于SysColumns 已弃用,请使用
Sys.All_Columns
:
Select
ObjectName = Object_Name(Object_ID)
,T.Name
,C.*
,T.*
From
Sys.All_Columns C
Inner Join Sys.Types T On T.User_Type_Id = C.User_Type_Id
Where [Object_ID] = Object_ID('Sys.Server_Permissions')
--Order By Name Asc
Select * From Sys.Types
will yield user_type_id = ID
of the type. Select * From Sys.Types
将产生user_type_id = ID
类型的user_type_id = ID
。 This is unique within the database.这在数据库中是独一无二的。 For system data types:
user_type_id = system_type_id
.对于系统数据类型:
user_type_id = system_type_id
。
DECLARE @col NVARCHAR(MAX);
SELECT @col= COALESCE(@col, '') + ',' + COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_name = 'MxLocations';
SELECT @col;
IF you are working with postgresql there is a possibility that more than one schema may have table with same name in that case apply the below query如果您正在使用 postgresql,那么在这种情况下,可能会有多个架构具有相同名称的表,请应用以下查询
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'your_table_name' AND table_schema = 'your_schema_name’;
set fmtonly on
select * from yourTable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.