SELECT F_LIMS_GET_SQL_FOR_MYSQL('tablename',0) FROM DUAL;
CREATE OR REPLACE FUNCTION F_LIMS_GET_SQL_FOR_MYSQL(PI_TABLENAME IN VARCHAR2,
PI_ISDROP IN INTEGER := 1)
RETURN CLOB IS
FunctionResult clob;
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_NormalINDEX 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(LISTAGG('`' || A.COLUMN_NAME || '` ') within group (order by A.COLUMN_NAME))
SELECT LISTAGG('`' || A.COLUMN_NAME || '` ',',') within group (order by A.position desc)
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 LISTAGG(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' AND NVL(T.DATA_SCALE,0) = 0 THEN
'INT'
WHEN T.DATA_TYPE = 'NUMBER' AND NVL(T.DATA_SCALE,0) != 0 THEN
'DECIMAL(' || T.DATA_PRECISION || ',' || T.DATA_SCALE || ')'
WHEN T.COLUMN_NAME = 'ID' THEN
'VARCHAR(32)'
ELSE
T.DATA_TYPE
END) || DECODE(T.NULLABLE, 'N', ' NOT NULL ', '') ||
' COMMENT ''' || TC.COMMENTS || ''',')
WITHIN GROUP (ORDER BY T.COLUMN_ID)
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_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 (LISTAGG('@ALTER TABLE `' || A.TABLE_NAME ||
'` ADD UNIQUE INDEX (' ||
LISTAGG('`' || A.COLUMN_NAME || '`',',') within group (order by a.position) || ');@')
within group (order by a.TABLE_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;
-- 普通索引
SELECT (LISTAGG('@ALTER TABLE `' || A.TABLE_NAME ||
'` ADD INDEX (' ||
LISTAGG('`' || A.COLUMN_NAME || '`',',') within group (order by A.column_position) || ');@')
within group (order by a.TABLE_NAME))
INTO V_SQL_NormalINDEX
FROM
user_ind_columns A
WHERE table_name =V_TABLENAME and SUBSTR(A.INDEX_NAME , 1, 2)<>'PK'
group by A.TABLE_NAME ,A.index_name;
IF V_SQL_NormalINDEX IS NOT NULL THEN
V_SQL_NormalINDEX := REPLACE(REPLACE(V_SQL_NormalINDEX, '@,@', CHR(13)), '@', '');
V_SQL_NormalINDEX := '-- 创建索引' || CHR(13) || V_SQL_NormalINDEX;
END IF;
RETURN V_SQL_HEADER || V_SQL_BODY || V_SQL_INDEX || V_SQL_NormalINDEX;
RETURN (FunctionResult);
END F_LIMS_GET_SQL_FOR_MYSQL;