SqlServer获取所有数据库,表,表结构
1 --获取所有数据库 2 SELECT * FROM Master..SysDatabases ORDER BY Name 3 4 --获取test数据库下所有表 5 use test SELECT name FROM SysObjects Where XType='U' ORDER BY Name 6 7 --获取test数据库下所有表结构信息 8 use test 9 SELECT (case when a.colorder=1 then d.name else null end) 表名, 10 a.colorder 字段序号,a.name 字段名, 11 (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 标识, 12 (case when (SELECT count(*) FROM sysobjects 13 WHERE (name in (SELECT name FROM sysindexes 14 WHERE (id = a.id) AND (indid in 15 (SELECT indid FROM sysindexkeys 16 WHERE (id = a.id) AND (colid in 17 (SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name))))))) 18 AND (xtype = 'PK'))>0 then '√' else '' end) 主键,b.name 类型,a.length 占用字节数, 19 COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度, 20 isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,(case when a.isnullable=1 then '√'else '' end) 允许空, 21 isnull(e.text,'') 默认值,isnull(g.[value], ' ') AS [说明] 22 FROM syscolumns a 23 left join systypes b on a.xtype=b.xusertype 24 inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties' 25 left join syscomments e on a.cdefault=e.id 26 left join sys.extended_properties g on a.id=g.major_id AND a.colid=g.minor_id 27 left join sys.extended_properties f on d.id=f.class and f.minor_id=0 28 where b.name is not null 29 --WHERE d.name='要查询的表' --如果只查询指定表,加上此条件 30 order by a.id,a.colorder