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

时间:2009-01-07 15:34:05

标签: sql tsql

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

使用这个,我希望能够遍历结果集(在C#中),然后对结果集中的每个表说,执行此操作(例如,使用反射来创建一个将执行或包含xyz的类)。

除此之外,SQL Server的一些优秀在线博客是什么?我知道这个问题实际上是关于在Sql Server中使用系统SP和数据库,我对一般查询没问题,所以我对一些涵盖这种功能的博客感兴趣。

由于

13 个答案:

答案 0 :(得分:143)

这就是你要找的东西:

使用OBJECT CATALOG VIEWS

 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/

答案 1 :(得分:33)

Tables ::

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

columns ::

SELECT * FROM INFORMATION_SCHEMA.COLUMNS 

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

答案 2 :(得分:12)

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

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' 

答案 3 :(得分:8)

SELECT * FROM INFORMATION_SCHEMA.COLUMNS

答案 4 :(得分:6)

我测试了一些找到的解决方案

Select *
From INFORMATION_SCHEMA.COLUMNS

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

Select *
From <DBNAME>.INFORMATION_SCHEMA.COLUMNS

,没有&lt;和&gt;,为您提供数据库DBNAME的列信息。

答案 5 :(得分:4)

你这里的其他内置朋友是系统sproc SP_HELP。

示例用法::

sp_help <MyTableName>

它返回的信息比您真正需要的信息多得多,但至少有90%的可能要求可以满足。

答案 6 :(得分:2)

把它扔出去 - 现在很容易复制/粘贴到word或google doc:

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

答案 7 :(得分:1)

这将为您提供所有用户创建的表格:

select * from sysobjects where xtype='U'

获得cols:

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

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

答案 8 :(得分:1)

对于MYSQL:

Select *
From INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = "<DatabaseName>"

答案 9 :(得分:0)

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

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

答案 10 :(得分:0)

我找到了一种使用SQL开发人员获取特定数据库的表和列的详细信息的简单方法。

Select *FROM USER_TAB_COLUMNS

答案 11 :(得分:0)

SELECT * FROM INFORMATION_SCHEMA.COLUMNS获取所有

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS获取所有表名。 在sqlserver上试试,

答案 12 :(得分:-1)

在Microsoft SQL Server中,您可以使用此功能:

declare @sql2 nvarchar(2000)
        set @sql2  ='
use ?
if (  db_name(db_id()) not in (''master'',''tempdb'',''model'',''msdb'',''SSISDB'')  )
begin   

select
    db_name() as db,
    SS.name as schemaname,
    SO.name tablename,
    SC.name columnname,
    ST.name type,
    case when ST.name in (''nvarchar'', ''nchar'')
        then convert(varchar(10), ( SC.max_length / 2 ))
        when ST.name in (''char'', ''varchar'')
        then convert(varchar(10), SC.max_length)
        else null
    end as length,
    case when SC.is_nullable = 0 then ''No'' when SC.is_nullable = 1 then ''Yes'' else null end as nullable,
    isnull(SC.column_id,0) as col_number
from sys.objects                  SO
join sys.schemas                  SS
    on SS.schema_id = SO.schema_id
join sys.columns             SC
on SO.object_id     = SC.object_id
left join sys.types               ST
    on SC.user_type_id = ST.user_type_id and SC.system_type_id = ST.system_type_id
    where SO.is_ms_shipped = 0 
end
'

exec sp_msforeachdb @command1 = @sql2

这将显示所有用户定义的数据库中的所有表和列(及其定义)。