获取表的相关信息Sqlserver

SELECT tableName, colOrder, colName, IsIdentity, pKey, type, bits, length, digit, IsNullAble, defaultValue, memo,ltrim(rtrim(tableName))+'.'+ltrim(rtrim(colName)) as col from
(
SELECT
       d.name as tableName,
       a.colorder as colOrder,
       a.name  as colName,
       (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then 1 else 0 end) as 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 1 else 0 end) pKey,--0:不是,1:是
       b.name type,--数据类型
       a.length bits,--字节数
       COLUMNPROPERTY(a.id,a.name,'PRECISION') as length,--长度
       isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as digit,--小数位数
       (case when a.isnullable=1 then 0 else 1 end) as IsNullAble,--允许空0:允许,1:不允许
       isnull(e.text,'') defaultValue,--默认值
       isnull(g.[value],'') AS memo--字段说明  
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 
) as SystemInfo
posted @ 2007-02-14 14:24  kuailewangzi1212  阅读(209)  评论(0编辑  收藏  举报