码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2020-02-18 20:46:45      阅读:27      评论:0      收藏:0      [点我收藏+]

标签:min   har   databases   创建   end   HERE   sys   serve   cte   

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中获取所有数据库名、表名、字段名以及描述

标签:min   har   databases   创建   end   HERE   sys   serve   cte   

原文地址:https://www.cnblogs.com/lang-lang/p/12327615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!