在数据库中获取表和字段的列表

我正在创build一个基本的ORM(纯粹是为了好玩),并想知道,是否有办法返回数据库中的表的列表,还有每个表的字段?

使用这个,我想能够遍历结果集(在C#中),然后说结果集中的每个表,做到这一点(例如使用reflection来做一个类,将做或包含xyz)。

除此之外,SQL Server有哪些好的在线博客? 我知道这个问题实际上是关于在Sql Server中使用系统SP和数据库的问题,我对一般的查询很满意,所以我对一些涵盖这种function的博客感兴趣。

谢谢

这是你想要的:

使用对象目录视图

SELECT T.name AS Table_Name , C.name AS Column_Name , P.name AS Data_Type , P.max_length AS Size , CAST(P.precision AS VARCHAR) + '/' + CAST(P.scale AS VARCHAR) AS Precision_Scale FROM sys.objects AS T JOIN sys.columns AS C ON T.object_id = C.object_id JOIN sys.types AS P ON C.system_type_id = P.system_type_id WHERE T.type_desc = 'USER_TABLE'; 

使用信息模式视图

  SELECT TABLE_SCHEMA , TABLE_NAME , COLUMN_NAME , ORDINAL_POSITION , COLUMN_DEFAULT , DATA_TYPE , CHARACTER_MAXIMUM_LENGTH , NUMERIC_PRECISION , NUMERIC_PRECISION_RADIX , NUMERIC_SCALE , DATETIME_PRECISION FROM INFORMATION_SCHEMA.COLUMNS; 

参考:我的博客 – http://dbalink.wordpress.com/2008/10/24/querying-the-object-catalog-and-information-schema-views/

表::

 SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' 

列 ::

 SELECT * FROM INFORMATION_SCHEMA.COLUMNS 

要么

 SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='your_table_name' 

获取数据库中所有表和字段的列表:

 Select * From INFORMATION_SCHEMA.COLUMNS Where TABLE_CATALOG Like 'DatabaseName' 

获取表中所有字段的列表:

 Select * From INFORMATION_SCHEMA.COLUMNS Where TABLE_CATALOG Like 'DatabaseName' And TABLE_NAME Like 'TableName' 
 SELECT * FROM INFORMATION_SCHEMA.COLUMNS 

您的其他内置朋友是系统spro SP_HELP。

样本用法::

 sp_help <MyTableName> 

它会返回比您真正需要更多的信息,但至less有90%的可能的要求将被照顾。

我testing了一些发现的解决scheme

 Select * From INFORMATION_SCHEMA.COLUMNS 

为您提供CURRENT /默认数据库的列信息。

 Select * From <DBNAME>.INFORMATION_SCHEMA.COLUMNS 

,没有<和>,给你数据库DBNAME的列信息。

这将使您获得所有用户创build的表格:

 select * from sysobjects where xtype='U' 

要获得cols:

 Select * from Information_Schema.Columns Where Table_Name = 'Insert Table Name Here' 

另外,我发现http://www.sqlservercentral.com/是一个很好的数据库资源。;

INFORMATION_SCHEMA方法的一个优点是它可以跨不同的数据库进行移植。

只是扔在那里 – 容易现在复制/粘贴到一个单词或谷歌文档:

 PRINT '<html><body>' SET NOCOUNT ON DECLARE @tableName VARCHAR(30) DECLARE tableCursor CURSOR LOCAL FAST_FORWARD FOR SELECT T.name AS TableName FROM sys.objects AS T WHERE T.type_desc = 'USER_TABLE' ORDER BY T.name OPEN tableCursor FETCH NEXT FROM tableCursor INTO @tableName WHILE @@FETCH_STATUS = 0 BEGIN PRINT '<h2>' + @tableName + '</h2>' PRINT '<pre>' SELECT LEFT(C.name, 30) AS ColumnName, LEFT(ISC.DATA_TYPE, 10) AS DataType, C.max_length AS Size, CAST(P.precision AS VARCHAR(4)) + '/' + CAST(P.scale AS VARCHAR(4)) AS PrecScale, CASE WHEN C.is_nullable = 1 THEN 'Null' ELSE 'No Null' END AS [Nullable], LEFT(ISNULL(ISC.COLUMN_DEFAULT, ' '), 5) AS [Default], CASE WHEN C.is_identity = 1 THEN 'Identity' ELSE '' END AS [Identity] FROM sys.objects AS T JOIN sys.columns AS C ON T.object_id = C.object_id JOIN sys.types AS P ON C.system_type_id = P.system_type_id JOIN INFORMATION_SCHEMA.COLUMNS AS ISC ON T.name = ISC.TABLE_NAME AND C.name = ISC.COLUMN_NAME WHERE T.type_desc = 'USER_TABLE' AND T.name = @tableName ORDER BY T.name, ISC.ORDINAL_POSITION PRINT '</pre>' FETCH NEXT FROM tableCursor INTO @tableName END CLOSE tableCursor DEALLOCATE tableCursor SET NOCOUNT OFF PRINT '</body></html>' 

我发现一个简单的方法来使用SQL开发人员获取特定数据库的表和列的细节。

 Select *FROM USER_TAB_COLUMNS 

这将返回由数据库参数指定的列的数据库名称,表名称,列名称和数据types:

 declare @database nvarchar(25) set @database = '' SELECT cu.table_catalog,cu.VIEW_SCHEMA, cu.VIEW_NAME, cu.TABLE_NAME, cu.COLUMN_NAME,c.DATA_TYPE,c.character_maximum_length from INFORMATION_SCHEMA.VIEW_COLUMN_USAGE as cu JOIN INFORMATION_SCHEMA.COLUMNS as c on cu.TABLE_SCHEMA = c.TABLE_SCHEMA and c.TABLE_CATALOG = cu.TABLE_CATALOG and c.TABLE_NAME = cu.TABLE_NAME and c.COLUMN_NAME = cu.COLUMN_NAME where cu.TABLE_CATALOG = @database order by cu.view_name,c.COLUMN_NAME