如何在SQL中获取所有表和列名?

时间:2018-02-27 16:57:10

标签: sql

我想要该表中的表名和列的列表(见下文)。 是否有SQL查询允许我在模式中执行此操作? Result I want
我知道我可以查看GUI界面来查看表名和列,但手动查看的内容太多了。

2 个答案:

答案 0 :(得分:2)

但是你的问题还不够明确,但你可以用这个代码获得所有这些

SELECT * FROM INFORMATION_SCHEMA.COLUMNS

答案 1 :(得分:0)

使用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;

取自这个答案: Getting list of tables, and fields in each, in a database