oracle表导出mysql适用的脚本方法

oracle表导出mysql适用的脚本方法
1.在对应的oracle数据库plsql中执行下面代码,建F_LIMS_GET_SQL_FOR_MYSQL函数
CREATE OR REPLACE FUNCTION F_LIMS_GET_SQL_FOR_MYSQL(PI_TABLENAME IN VARCHAR2,
PI_ISDROP IN INTEGER := 1)
RETURN CLOB IS
V_TABLENAME VARCHAR(100);
V_RET_SQL VARCHAR2(4000);
V_SQL_HEADER VARCHAR2(1000);
V_SQL_BODY CLOB;
V_SQL_INDEX VARCHAR2(4000);
V_SQL_TABLE_COMMENT VARCHAR(400);
V_SQL_PK_COL_LIST VARCHAR(1000);
-- 根据当前数据库和表名,生成mysql建表语句
BEGIN
-- 表名统一为大写
SELECT UPPER(PI_TABLENAME) INTO V_TABLENAME FROM DUAL;

SELECT MAX(T.COMMENTS)
INTO V_SQL_TABLE_COMMENT
FROM USER_TAB_COMMENTS T
WHERE T.TABLE_NAME = V_TABLENAME;

SELECT MAX(WM_CONCAT('`' || A.COLUMN_NAME || '` '))
INTO V_SQL_PK_COL_LIST
FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B
WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
AND B.CONSTRAINT_TYPE = 'P'
AND A.TABLE_NAME = V_TABLENAME
GROUP BY A.TABLE_NAME;

V_SQL_HEADER := '-- ' || V_SQL_TABLE_COMMENT || ' 建表语句' || CHR(13);

IF PI_ISDROP = 1 THEN
V_SQL_HEADER := V_SQL_HEADER || 'DROP TABLE IF EXISTS `' || V_TABLENAME || '`;' ||
CHR(13);
END IF;
V_SQL_HEADER := V_SQL_HEADER || 'CREATE TABLE `' || V_TABLENAME || '` (';

-- 表列信息
SELECT WM_CONCAT(CHR(13) || '`' || T.COLUMN_NAME || '` ' ||
(CASE
WHEN T.DATA_TYPE = 'VARCHAR2' OR T.DATA_TYPE = 'NVARCHAR2' THEN
'VARCHAR(' || T.DATA_LENGTH || ')'
WHEN T.DATA_TYPE = 'DATE' THEN
'DATETIME'
WHEN T.DATA_TYPE = 'CLOB' THEN
'MEDIUMTEXT'
WHEN T.DATA_TYPE = 'NUMBER' THEN
'DECIMAL'
WHEN T.COLUMN_NAME = 'ID' THEN
'VARCHAR(32)'
ELSE
T.DATA_TYPE
END) || DECODE(T.NULLABLE, 'N', ' NOT NULL ', '') ||
' COMMENT ''' || TC.COMMENTS || '''')
INTO V_SQL_BODY
FROM USER_TAB_COLUMNS T
LEFT JOIN USER_COL_COMMENTS TC
ON T.TABLE_NAME = TC.TABLE_NAME
AND T.COLUMN_NAME = TC.COLUMN_NAME
WHERE T.TABLE_NAME = V_TABLENAME
ORDER BY T.COLUMN_ID;

IF V_SQL_PK_COL_LIST IS NOT NULL THEN
V_SQL_BODY := V_SQL_BODY || ' ,' || CHR(13) || ' PRIMARY KEY (' ||
V_SQL_PK_COL_LIST || ') ';
END IF;

-- 表备注
V_SQL_BODY := V_SQL_BODY || CHR(13) || ') COMMENT = ''' ||
V_SQL_TABLE_COMMENT || ''';' || CHR(13);

-- 唯一索引
SELECT (WM_CONCAT('@ALTER TABLE `' || A.TABLE_NAME ||
'` ADD UNIQUE INDEX (' ||
WM_CONCAT('`' || A.COLUMN_NAME || '`') || ');@'))
INTO V_SQL_INDEX
FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B
WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
AND B.CONSTRAINT_TYPE = 'U'
AND A.TABLE_NAME = V_TABLENAME
GROUP BY A.TABLE_NAME, A.CONSTRAINT_NAME;

IF V_SQL_INDEX IS NOT NULL THEN
V_SQL_INDEX := REPLACE(REPLACE(V_SQL_INDEX, '@,@', CHR(13)), '@', '');
V_SQL_INDEX := '-- 创建索引' || CHR(13) || V_SQL_INDEX;
END IF;

RETURN V_SQL_HEADER || V_SQL_BODY || V_SQL_INDEX;
END;


2.执行下面代码一个个导出表结构脚本
SELECT F_LIMS_GET_SQL_FOR_MYSQL('表名',0) FROM DUAL;

posted @ 2024-05-07 15:31  南国之恋  阅读(30)  评论(0编辑  收藏  举报