欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL2005查询表结构的SQL语句使用分享

程序员文章站 2023-10-31 09:07:58
复制代码 代码如下:select    (case when a.colorder=1 then d.name else '' end) as...

复制代码 代码如下:

select   
(case when a.colorder=1 then d.name else '' end) as 表名,--如果表名相同就返回空   syscolumns(表字段信息表) a  sysobjects d
     a.colorder as 字段序号,  
     a.name as 字段名,  
     (case when columnproperty( a.id,a.name, 'isidentity' )=1 then '√' else '' end) as 标识,   --返回isidentity的值,isidentity只有两个值:0、1
     (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) as 主键,--查询主键end  
b.name as 类型,   --systypes b
a.length as 占用字节数,  
columnproperty(a.id,a.name,'precision' ) as    长度,  
isnull(columnproperty(a.id,a.name,'scale' ),0) as 小数位数,  
(case when a.isnullable=1 then '√' else '' end) as 允许空,  
isnull(e.text,'' ) as 默认值,   --syscomments e
isnull(g.[value],'' ) as 字段说明    --sys.extended_properties g (字段信息表)
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 sys.extended_properties g  
on a.id=g.major_id and a.colid = g.minor_id   
     where d.name='systempara' --所要查询的表  
order by a.id,a.colorder