MSSQL server 查询表结构
SELECT
(CASE WHEN a.colorder=1 THEN d.name ELSE '' END) N'表名',
a.colorder N'字段序号',
a.name N'字段名',
ISNULL(g.[value],'') AS N'字段说明',
(CASE WHEN COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 THEN '√'ELSE '' END) N'标识',
(CASE WHEN (SELECT COUNT(*)
FROM sysobjects
WHERE (name in
(SELECT name
FROM sysindexes
WHERE (id = a.id) AND (indid in
(SELECT indid
FROM sysindexkeys
WHERE (id = a.id) AND (colid in
(SELECT colid
FROM syscolumns
WHERE (id = a.id) AND (name = a.name))))))) AND
(xtype = 'PK'))>0 THEN '√' ELSE '' END) N'主键',
b.name N'类型',
a.length N'占用字节数',
COLUMNPROPERTY(a.id,a.name,'PRECISION') AS N'长度',
ISNULL(COLUMNPROPERTY(a.id,a.name,'Scale'),0) AS N'小数位数',
(CASE WHEN a.isnullable=1 THEN '√'ELSE '' END) N'允许空',
ISNULL(e.text,'') N'默认值'
FROM syscolumns a
LEFT JOIN systypes b on a.xtype=b.xusertype
INNER JOIN sysobjects d on a.id=d.id AND d.xtype='U' AND d.name<>'dtproperties'
LEFT JOIN syscomments e on a.cdefault=e.id
LEFT JOIN sysproperties g on a.id=g.id AND a.colid=g.smallid
WHERE
d.name = 'qxcl' --要查询的表
ORDER BY
object_name(a.id), a.colorder
----
上面代码由 =subsky= 提供, 可以得到漂亮的格式.
简单点的:
select a.name, a.ColID, a.usertype, b.name, a.length, a.prec, a.scale, a.Status, a.cDefault,
case a.cdefault when 0 then ' ' else (select c.text from syscomments c where a.cdefault = c.id) end const_key
from syscolumns a, systypes b
where object_name(a.id) = 'XXXX' --表名
and a.xusertype = b.xusertype and a.usertype = b.usertype
order by a.ColID
代码由 XXL 提供
网上搜的:
SELECT column_name,column_type,is_nullable,column_key,col umn_default,extra FROM information_schema.COLUMNS WHERE
table_schema='' AND table_name=''
|