<经验杂谈>查询表结构的SQL语句

时间:2023-03-09 04:41:17
<经验杂谈>查询表结构的SQL语句

在我们使用SQL数据库的过程中,经常会遇到查询表结构的情况,以下就是sql语句的写法:

--查询非系统数据库

SELECT name
FROM Master..SysDatabases
WHERE dbid > 4

查询数据库下所有表

 SELECT name
FROM sysobjects
WHERE xtype = 'U'
OR xtype = 'S'

查询所有用户表

 SELECT name
FROM sysobjects
WHERE xtype = 'U'
AND OBJECTPROPERTY(id, 'IsMSShipped') = 0

查询表的相关信息(字段名、长度、类型、字段说明)

SELECT a.[name] AS '字段名' ,
a.length '长度' ,
c.[name] '类型' ,
e.value AS '字段说明'
FROM syscolumns a
LEFT JOIN systypes b ON a.xusertype = b.xusertype
LEFT JOIN systypes c ON a.xtype = c.xusertype
INNER JOIN sysobjects d ON a.id = d.id
AND d.xtype = 'U'
LEFT JOIN sys.extended_properties e ON a.id = e.major_id
AND a.colid = e.minor_id
AND e.name = 'MS_Description'
WHERE d.name = 'Comment'