sql获取表结构

Posted 递归中的DOM

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql获取表结构相关的知识,希望对你有一定的参考价值。

SELECT a.name 字段名,
            (case when COLUMNPROPERTY( a.id,a.name,IsIdentity)=1 then 1 ELSE 0 end) 标识, 
            (case when (SELECT count(*) FROM dbo.sysobjects  
            WHERE (name in (SELECT name FROM dbo.sysindexes  
            WHERE (id = a.id) AND (indid in  
            (SELECT indid FROM dbo.sysindexkeys  
            WHERE (id = a.id) AND (colid in  
            (SELECT colid FROM dbo.syscolumns WHERE (id = a.id) AND (name = a.name)))))))  
            AND (xtype = PK))>0 then 1 else 0 end) 主键,b.name 类型,
            h.CHARACTER_MAXIMUM_LENGTH as 长度,h.NUMERIC_PRECISION as 数字长度,
            isnull(h.NUMERIC_SCALE,0) as 小数位数,(case when a.isnullable=1 then 1 ELSE 0 end) 允许空,  
            isnull(e.text,‘‘) 默认值,isnull(g.[value],  ) AS [说明]
            FROM  dbo.syscolumns a 
            left join dbo.systypes b on a.xtype=b.xusertype  
            inner join dbo.sysobjects d on a.id=d.id and d.xtype=U and d.name<>dtproperties 
            left join dbo.syscomments e on a.cdefault=e.id  
            left join sys.extended_properties g on a.id=g.major_id AND a.colid=g.minor_id
            left join sys.extended_properties f on d.id=f.class and f.minor_id=0
            LEFT JOIN information_schema.COLUMNS h ON h.TABLE_NAME = d.name AND h.COLUMN_NAME = a.name
            --where b.name is not null
            WHERE d.name=StockInfo --如果只查询指定表,加上此条件
            order by a.id,a.colorder

 

以上是关于sql获取表结构的主要内容,如果未能解决你的问题,请参考以下文章

分享几个实用的代码片段(第二弹)

分享几个实用的代码片段(第二弹)

MySQL如何使用SQL语句获取表结构和获取全部表名

MySQL如何使用SQL语句获取表结构和获取全部表名

获取临时表的结构(如生成 sql 脚本)并清除当前实例的临时表

sql Closure表操作SQL片段