码迷,mamicode.com
首页 > 数据库 > 详细

SQLServer2005,2000获取表结构:字段名、类型、长度、主键、非空、注释

时间:2014-07-13 08:58:15      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   div   

SQLServer 2005

SELECT
    d.name NTableName,
    d.xtype NTableType,
    a.colorder NColumnIndex,
    a.name NColumnName,
    (case when COLUMNPROPERTY( a.id,a.name,IsIdentity)=1 then 1else 0 end) NIdnetityFG,
    (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 1 else 0 end
    ) NIsPK,
    b.name NDataType,
    COLUMNPROPERTY(a.id,a.name,PRECISION) as NLength,
    (case when a.isnullable=1 then 1else 0 end) NIsNullable,
    isnull(e.text,‘‘) NDefault,
    g.value NDescription    
FROM   syscolumns   a 
    left join systypes b on   a.xtype=b.xusertype
    inner join sysobjects d on a.id=d.id   and   d.xtype in (U,V) and   d.name<>dtproperties
    left join syscomments e on a.cdefault=e.id
    LEFT OUTER JOIN sys.extended_properties AS g ON a.id = g.major_id AND a.colid = g.minor_id
order by object_name(a.id),a.colorder

 

SQLServer2000

SELECT
    d.name NTableName,
    d.xtype NTableType,
    a.colorder NColumnIndex,
    a.name NColumnName,
    (case when COLUMNPROPERTY( a.id,a.name,IsIdentity)=1 then 1else 0 end) NIdnetityFG,
    (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 1 else 0 end
    ) NIsPK,
    b.name NDataType,
    COLUMNPROPERTY(a.id,a.name,PRECISION) as NLength,
    (case when a.isnullable=1 then 1else 0 end) NIsNullable,
    isnull(e.text,‘‘) NDefault,
    g.value NDescription    
FROM   syscolumns   a 
    left join systypes b on   a.xtype=b.xusertype
    inner join sysobjects d on a.id=d.id   and   d.xtype in (U,V) 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
order by object_name(a.id),a.colorder

--select * from sysproperties

SQLServer2005,2000获取表结构:字段名、类型、长度、主键、非空、注释,布布扣,bubuko.com

SQLServer2005,2000获取表结构:字段名、类型、长度、主键、非空、注释

标签:des   style   blog   color   io   div   

原文地址:http://www.cnblogs.com/xiaotiannet/p/3838150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!