MSSQL 查询表结构
作者:互联网
-- 查询 数据库 视图
SELECT * FROM sysdatabases ;
-- 查询 表 视图
SELECT * from sysobjects
WHERE 1 = 1 AND XTYPE IN ('U','V')
order by name;
/**
* 查询 列 视图
* OBJECT_Id: sysobjects.Id 可以通过 object_id('你的表名') 转换为id
*/
SELECT * FROM syscolumns ;
/**
* 查询 注释 视图
* major_id: sysobjects.Id
* minor_id: 0 ? 表注释 : 对应的列注释[syscolumns.Column_Id]
*/
SELECT * FROM sys.extended_properties;
/**
* 主键
*/
SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
--WHERE TABLE_NAME='PDA_xjData'
/**
* 查询表结构
*/
SELECT O.name AS 'TableName', C.name AS 'ColumnName',T.name AS 'Type'
, C.IsNullAble
, CASE T.name WHEN 'char' THEN C.prec WHEN 'varchar' THEN C.prec WHEN 'nvarchar' THEN C.prec
WHEN 'text' THEN C.prec WHEN 'ntext' THEN C.prec
WHEN 'decimal' THEN C.xprec
ELSE C.length END AS 'Length'
, C.scale
, C.id TableId
, C.colid ColumnId
, S.value AS Comment
, ST.value AS TableComment
, CASE WHEN K.COLUMN_NAME IS NULL THEN 0 ELSE 1 END IsPk
, C.*
FROM sysobjects O
INNER JOIN syscolumns C ON C.id = O.id
INNER JOIN systypes T ON T.xusertype = C.xusertype
INNER JOIN sys.extended_properties S ON S.major_id = C.id AND S.minor_id = C.colid
LEFT JOIN sys.extended_properties ST ON ST.major_id = C.id AND ST.minor_id = 0
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE K ON K.TABLE_NAME = O.name AND K.COLUMN_NAME = C.name
WHERE 1 = 1
AND O.XTYPE IN ('U','V')
ORDER BY O.name, C.colid;
标签:JOIN,name,WHEN,prec,MSSQL,查询,id,SELECT,结构 来源: https://www.cnblogs.com/Cailf/p/15094533.html