SQLServer中获取所有数据库名表名字段名以及描述

Posted lang-lang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQLServer中获取所有数据库名表名字段名以及描述相关的知识,希望对你有一定的参考价值。

1. 获取所有的数据库的信息

SELECT * FROM MASTER.DBO.SYSDATABASES

2. 获取所有数据表名

XTYPE

  • U 代表用户创建的表
  • S 代表系统表
SELECT * FROM sys.SYSOBJECTS WHERE XTYPE=U

3. 查看某表内的所有列

SELECT * FROM sys.SYSCOLUMNS WHERE ID IN (SELECT ID FROM sys.SYSOBJECTS WHERE XTYPE=U AND NAME = 表名)

4. 查看某表内的所有列(包含描述)

SELECT * FROM sys.extended_properties
LEFT JOIN sys.SYSOBJECTS ON sys.SYSOBJECTS.id = sys.extended_properties.major_id
LEFT JOIN INFORMATION_SCHEMA.columns ON (INFORMATION_SCHEMA.columns.ORDINAL_POSITION = sys.extended_properties.minor_id AND INFORMATION_SCHEMA.columns.TABLE_NAME = sys.SYSOBJECTS.name)
WHERE major_id IN (SELECT ID FROM sys.SYSOBJECTS WHERE XTYPE=U AND NAME = 表名) AND INFORMATION_SCHEMA.columns.TABLE_NAME is not null
-- 提取适当的列
SELECT
INFORMATION_SCHEMA.columns.TABLE_CATALOG AS 数据库名,
INFORMATION_SCHEMA.columns.TABLE_NAME AS 表名,
INFORMATION_SCHEMA.columns.COLUMN_NAME AS 列名,
INFORMATION_SCHEMA.columns.DATA_TYPE AS 数据类型,
INFORMATION_SCHEMA.columns.CHARACTER_MAXIMUM_LENGTH AS varchar长度,
sys.extended_properties.value AS 描述
FROM sys.extended_properties
LEFT JOIN sys.SYSOBJECTS ON sys.SYSOBJECTS.id = sys.extended_properties.major_id
LEFT JOIN INFORMATION_SCHEMA.columns ON (INFORMATION_SCHEMA.columns.ORDINAL_POSITION = sys.extended_properties.minor_id AND INFORMATION_SCHEMA.columns.TABLE_NAME = sys.SYSOBJECTS.name)
WHERE major_id IN (SELECT ID FROM sys.SYSOBJECTS WHERE XTYPE=U AND NAME = ‘表名) AND INFORMATION_SCHEMA.columns.TABLE_NAME is not null

5.  获取数据库中所有的表(包含描述)

SELECT * FROM sys.extended_properties
LEFT JOIN sys.SYSOBJECTS ON sys.SYSOBJECTS.id = sys.extended_properties.major_id
LEFT JOIN INFORMATION_SCHEMA.columns ON (INFORMATION_SCHEMA.columns.ORDINAL_POSITION = sys.extended_properties.minor_id AND INFORMATION_SCHEMA.columns.TABLE_NAME = sys.SYSOBJECTS.name)
WHERE major_id IN (SELECT ID FROM sys.SYSOBJECTS WHERE XTYPE=U) AND INFORMATION_SCHEMA.columns.TABLE_NAME is null
-- 提取适当的列
SELECT
INFORMATION_SCHEMA.columns.TABLE_CATALOG AS 数据库名,
sys.SYSOBJECTS.name AS 表名,
INFORMATION_SCHEMA.columns.COLUMN_NAME AS 列名,
INFORMATION_SCHEMA.columns.DATA_TYPE AS 数据类型,
INFORMATION_SCHEMA.columns.CHARACTER_MAXIMUM_LENGTH AS varchar长度,
sys.extended_properties.value AS 描述
FROM sys.extended_properties
LEFT JOIN sys.SYSOBJECTS ON sys.SYSOBJECTS.id = sys.extended_properties.major_id
LEFT JOIN INFORMATION_SCHEMA.columns ON (INFORMATION_SCHEMA.columns.ORDINAL_POSITION = sys.extended_properties.minor_id AND INFORMATION_SCHEMA.columns.TABLE_NAME = sys.SYSOBJECTS.name)
WHERE major_id IN (SELECT ID FROM sys.SYSOBJECTS WHERE XTYPE=U) AND INFORMATION_SCHEMA.columns.TABLE_NAME is null

 

以上是关于SQLServer中获取所有数据库名表名字段名以及描述的主要内容,如果未能解决你的问题,请参考以下文章

MYSQL 数据库名表名字段名查询

SQL注入

Oracle 查询库中所有表名字段名字段名说明,查询表的数据条数表名中文表名

MySQL运维及开发规范

数据库的表名字段名大小写问题

PostgreSQL中表名字段名大小写问题