获取列的列 table
Get columns of the columns table
如何获取 INFORMATION_SCHEMA.COLUMNS
视图的列列表?
我可以获取我创建的任何 table 的列:
select *
from information_schema.columns
where table_name = 'MY_TABLE'
结果集包含多列,如:
TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
DATA_TYPE
- 等等
如何获得这些列的完整列表? COLUMNS
视图似乎不包含其列 本身 (如在 Oracle、PostgreSQL 或 MySQL 中)。如果我 运行:
select *
from information_schema.columns
where table_name = 'COLUMNS'
我一无所获。可能我找错地方了。
使用sys.dm_exec_describe_first_result_set:
SELECT *
FROM sys.dm_exec_describe_first_result_set(
N'SELECT * FROM INFORMATION_SCHEMA.COLUMNS', NULL, 0);
SQL 服务器在 sys
模式下保存其元数据。
INFORMATION_SCHEMA
后来作为 SQL ISO 合规性的一部分添加。
select s.name as schema_name
,o.name as table_name
,c.name as column_name
,t.name as type_name
from sys.schemas s
join sys.all_objects o on o.schema_id = s.schema_id
join sys.all_columns c on c.object_id = o.object_id
join sys.types t on t.system_type_id = c.system_type_id and t.user_type_id = c.user_type_id
where s.name = 'INFORMATION_SCHEMA'
and o.name = 'COLUMNS'
.
+--------------------+------------+--------------------------+-----------+
| schema_name | table_name | column_name | type_name |
+--------------------+------------+--------------------------+-----------+
| INFORMATION_SCHEMA | COLUMNS | TABLE_CATALOG | nvarchar |
| INFORMATION_SCHEMA | COLUMNS | TABLE_SCHEMA | nvarchar |
| INFORMATION_SCHEMA | COLUMNS | TABLE_NAME | sysname |
| INFORMATION_SCHEMA | COLUMNS | COLUMN_NAME | sysname |
| INFORMATION_SCHEMA | COLUMNS | ORDINAL_POSITION | int |
| INFORMATION_SCHEMA | COLUMNS | COLUMN_DEFAULT | nvarchar |
| INFORMATION_SCHEMA | COLUMNS | IS_NULLABLE | varchar |
| INFORMATION_SCHEMA | COLUMNS | DATA_TYPE | nvarchar |
| INFORMATION_SCHEMA | COLUMNS | CHARACTER_MAXIMUM_LENGTH | int |
| INFORMATION_SCHEMA | COLUMNS | CHARACTER_OCTET_LENGTH | int |
| INFORMATION_SCHEMA | COLUMNS | NUMERIC_PRECISION | tinyint |
| INFORMATION_SCHEMA | COLUMNS | NUMERIC_PRECISION_RADIX | smallint |
| INFORMATION_SCHEMA | COLUMNS | NUMERIC_SCALE | int |
| INFORMATION_SCHEMA | COLUMNS | DATETIME_PRECISION | smallint |
| INFORMATION_SCHEMA | COLUMNS | CHARACTER_SET_CATALOG | sysname |
| INFORMATION_SCHEMA | COLUMNS | CHARACTER_SET_SCHEMA | sysname |
| INFORMATION_SCHEMA | COLUMNS | CHARACTER_SET_NAME | sysname |
| INFORMATION_SCHEMA | COLUMNS | COLLATION_CATALOG | sysname |
| INFORMATION_SCHEMA | COLUMNS | COLLATION_SCHEMA | sysname |
| INFORMATION_SCHEMA | COLUMNS | COLLATION_NAME | sysname |
| INFORMATION_SCHEMA | COLUMNS | DOMAIN_CATALOG | sysname |
| INFORMATION_SCHEMA | COLUMNS | DOMAIN_SCHEMA | sysname |
| INFORMATION_SCHEMA | COLUMNS | DOMAIN_NAME | sysname |
+--------------------+------------+--------------------------+-----------+
如何获取 INFORMATION_SCHEMA.COLUMNS
视图的列列表?
我可以获取我创建的任何 table 的列:
select *
from information_schema.columns
where table_name = 'MY_TABLE'
结果集包含多列,如:
TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
DATA_TYPE
- 等等
如何获得这些列的完整列表? COLUMNS
视图似乎不包含其列 本身 (如在 Oracle、PostgreSQL 或 MySQL 中)。如果我 运行:
select *
from information_schema.columns
where table_name = 'COLUMNS'
我一无所获。可能我找错地方了。
使用sys.dm_exec_describe_first_result_set:
SELECT *
FROM sys.dm_exec_describe_first_result_set(
N'SELECT * FROM INFORMATION_SCHEMA.COLUMNS', NULL, 0);
SQL 服务器在 sys
模式下保存其元数据。
INFORMATION_SCHEMA
后来作为 SQL ISO 合规性的一部分添加。
select s.name as schema_name
,o.name as table_name
,c.name as column_name
,t.name as type_name
from sys.schemas s
join sys.all_objects o on o.schema_id = s.schema_id
join sys.all_columns c on c.object_id = o.object_id
join sys.types t on t.system_type_id = c.system_type_id and t.user_type_id = c.user_type_id
where s.name = 'INFORMATION_SCHEMA'
and o.name = 'COLUMNS'
.
+--------------------+------------+--------------------------+-----------+
| schema_name | table_name | column_name | type_name |
+--------------------+------------+--------------------------+-----------+
| INFORMATION_SCHEMA | COLUMNS | TABLE_CATALOG | nvarchar |
| INFORMATION_SCHEMA | COLUMNS | TABLE_SCHEMA | nvarchar |
| INFORMATION_SCHEMA | COLUMNS | TABLE_NAME | sysname |
| INFORMATION_SCHEMA | COLUMNS | COLUMN_NAME | sysname |
| INFORMATION_SCHEMA | COLUMNS | ORDINAL_POSITION | int |
| INFORMATION_SCHEMA | COLUMNS | COLUMN_DEFAULT | nvarchar |
| INFORMATION_SCHEMA | COLUMNS | IS_NULLABLE | varchar |
| INFORMATION_SCHEMA | COLUMNS | DATA_TYPE | nvarchar |
| INFORMATION_SCHEMA | COLUMNS | CHARACTER_MAXIMUM_LENGTH | int |
| INFORMATION_SCHEMA | COLUMNS | CHARACTER_OCTET_LENGTH | int |
| INFORMATION_SCHEMA | COLUMNS | NUMERIC_PRECISION | tinyint |
| INFORMATION_SCHEMA | COLUMNS | NUMERIC_PRECISION_RADIX | smallint |
| INFORMATION_SCHEMA | COLUMNS | NUMERIC_SCALE | int |
| INFORMATION_SCHEMA | COLUMNS | DATETIME_PRECISION | smallint |
| INFORMATION_SCHEMA | COLUMNS | CHARACTER_SET_CATALOG | sysname |
| INFORMATION_SCHEMA | COLUMNS | CHARACTER_SET_SCHEMA | sysname |
| INFORMATION_SCHEMA | COLUMNS | CHARACTER_SET_NAME | sysname |
| INFORMATION_SCHEMA | COLUMNS | COLLATION_CATALOG | sysname |
| INFORMATION_SCHEMA | COLUMNS | COLLATION_SCHEMA | sysname |
| INFORMATION_SCHEMA | COLUMNS | COLLATION_NAME | sysname |
| INFORMATION_SCHEMA | COLUMNS | DOMAIN_CATALOG | sysname |
| INFORMATION_SCHEMA | COLUMNS | DOMAIN_SCHEMA | sysname |
| INFORMATION_SCHEMA | COLUMNS | DOMAIN_NAME | sysname |
+--------------------+------------+--------------------------+-----------+