查询sql数据库中的所有表和表描述

获取数据库中所有的表

select name as tablename, isnull((select  value from  sys.extended_properties where sysobjects.id=major_id and minor_id=0),'') as tabledesc
 from sysobjects where type='U' and name<>'sysdiagrams'

 

根据表明,获取字段名

SELECT
(case when a.colorder=1 then d.name else '' end) N'tablename',
a.colorder N'fieldindex',
a.name N'fieldname',
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '1'else '0'
end) N'IsIdentity',
(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) N'primarykey',
b.name N'type',
a.length N'size',
COLUMNPROPERTY(a.id,a.name,'PRECISION') as N'length',
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as N'Decimalplaces',
(case when a.isnullable=1 then '1'else '0' end) N'Allowair',
isnull(e.text,'') N'defaultvalue',
isnull(g.[value],'') AS N'desc'
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=@TableName
order by object_name(a.id),a.colorder

posted on 2012-03-09 20:30  水影倒影  阅读(799)  评论(0编辑  收藏  举报

导航