数据库 - 不同数据库将表、字段、描述保存到Excel
不同数据库将表、字段、描述保存到Excel
查询语句
SqlServer
SELECT 表名 = CASE WHEN A.COLORDER=1 THEN D.NAME ELSE '' END, 表备注 = CASE WHEN A.COLORDER=1 THEN ISNULL(F.VALUE,'') ELSE '' END, 列序号 = A.COLORDER, 列名称 = A.NAME, 标识 = CASE WHEN COLUMNPROPERTY(A.ID,A.NAME,'ISIDENTITY')=1 THEN '√ ' ELSE '' END, 主键 = CASE WHEN EXISTS(SELECT 1 FROM SYSOBJECTS WHERE XTYPE='PK' AND PARENT_OBJ=A.ID AND NAME IN ( SELECT NAME FROM SYSINDEXES WHERE INDID IN( SELECT INDID FROM SYSINDEXKEYS WHERE ID=A.ID AND COLID=A.COLID))) THEN '√' ELSE '' END, 类型 = B.NAME, 字节 = A.LENGTH, 长度 = COLUMNPROPERTY(A.ID,A.NAME,'PRECISION'), 小数位 = ISNULL(COLUMNPROPERTY(A.ID,A.NAME,'SCALE'),0), 允许空 = CASE WHEN A.ISNULLABLE=1 THEN '√ 'ELSE '' END, 默认值 = ISNULL(E.TEXT,''), 列备注 = ISNULL(G.[VALUE],'') FROM SYSCOLUMNS A LEFT JOIN SYSTYPES B ON A.XUSERTYPE=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 --where D.NAME='tbname' --查询这个表 ORDER BY A.ID,A.COLORDER
Oracle
SELECT A.COLUMN_NAME AS columnName, A.DATA_TYPE AS columnType, A.CHAR_LENGTH AS charLength, A.NULLABLE AS isNullAble, B.comments AS columnComment FROM all_tab_columns A, DBA_COL_COMMENTS B WHERE A.OWNER = B.OWNER AND A.table_name = B.table_name AND A.COLUMN_NAME = B.COLUMN_NAME AND A.OWNER = #{dbName} -- 数据库名 AND A .TABLE_NAME = #{tableName} -- 表名
MySQL
SELECT column_name as columnName, column_type as columnType, is_nullable as isNullAble, COLUMN_DEFAULT as columnDefault, column_comment as columnComment, CHARACTER_MAXIMUM_LENGTH as charLength FROM information_schema.COLUMNS WHERE table_schema = #{dbName} -- 数据库名 AND table_name = #{tableName} -- 表名
转换成Excel
将查询出来的结果以 CSV 格式导出,之后在Excel中找到 【数据】-【分列】,以逗号为分隔符分割,就可以了