查询并导出表结构为Excel
应公司要求将数据库结构用表格形式来展示给客户,最开始我手工弄了两张表效率实在太低了,于是就想偷懒了,就去网上找了一段儿sql查询语句效率提高了70%一执行就出来了,导出查询结果剩下的就只需要调整一下excel
SELECT 表名 = case when a.colorder=1 then d.name else '' end, 表说明 = case when a.colorder=1 then isnull(f.value,'') else '' end, 字段中文名称 = isnull(g.[value],''), 字段英文名称 = a.name, 类型 = b.name, 长度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'), --主键 = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in( --SELECT name FROM sysindexes WHERE indid in( --SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid --))) then '1' else '0' end, 允许空=case when a.isnullable=1 then '1'else '0' end 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 left join sys.extended_properties f on d.id=f.major_id and f.minor_id =0 order by a.id,a.colorder,d.name
所以该偷懒还是要偷懒,记录一下以后有机会给它写成一个工具。至于处于哪里我不小心关了页面(对不起了!博主,无意冒犯)