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
原文地址:https://www.cnblogs.com/lang-lang/p/12327615.html
时间: 2024-11-05 23:31:26