从数据库中获取表和列 - SQL Server

时间:2015-07-10 20:47:37

标签: sql-server

数据库abc,它包含2个表table1table2

我想有这样的事情:

table1

column1 int
column2 nvarchar(50)

表2

column1 int
column2 money
column3 bit

4 个答案:

答案 0 :(得分:4)

使用INFORMATION_SCHEMA架构及其中的对象,如

INFORMATION_SCHEMA.COLUMNS

具有生成所需输出所需的所有元数据。

答案 1 :(得分:2)

尝试使用INFORMATION_SCHEMA.COLUMNS

select * from INFORMATION_SCHEMA.COLUMNS
where TABLE_CATALOG='My_Database_Name_Here'

select TABLE_NAME,COLUMN_NAME,DATA_TYPE from INFORMATION_SCHEMA.COLUMNS
where TABLE_CATALOG='My_Database_Name_Here'

答案 2 :(得分:1)

你可以写一个像这样的查询:

USE abc --change this to whatever database you have your tables in

SELECT Table_Name
    ,Column_Name
    ,DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME IN ('table1','table2')

答案 3 :(得分:1)

SELECT 
    TABLE_NAME, COLUMN_NAME as [Column], 
    ISNULL(DATA_TYPE + '(' + cast(CHARACTER_MAXIMUM_LENGTH as nvarchar(10)) +')', DATA_TYPE) as [Data type] 
FROM 
    information_schema.COLUMNS