MSSQL 查询表结构
2021/8/3 19:07:44
本文主要是介绍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;
这篇关于MSSQL 查询表结构的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-08Docker下的SqlServer发布订阅启用
- 2023-06-05Docker安装MS SQL Server并使用Navicat远程连接
- 2023-05-25深入浅出 SQL Server CDC 数据同步
- 2023-05-12通过空间占用和执行计划了解SQL Server的行存储索引
- 2023-04-24以SQLserver为例的Dapper详细讲解
- 2022-11-30SQL server高级函数查询
- 2022-11-26SQL SERVER数据库服务器CPU不能全部利用原因分析
- 2022-11-21SQL Server 时间算差值/常用函数
- 2022-11-20调试Archery连接SQL Server提示驱动错误
- 2022-10-22SQL Server 完整、差异备份+完整、差异还原(详细讲解,规避错误)