create table Z_TABLE_INFO
(
dept_name VARCHAR2(20),
table_name VARCHAR2(30),
column_id NUMBER,
column_comment VARCHAR2(50),
column_name VARCHAR2(30),
column_type VARCHAR2(50)
);
insert into Z_TABLE_INFO (DEPT_NAME, TABLE_NAME, COLUMN_ID, COLUMN_COMMENT, COLUMN_NAME, COLUMN_TYPE) values ('表A', 'TABLE_A', 1, '序号', 'XH', 'NUMBER');
insert into Z_TABLE_INFO (DEPT_NAME, TABLE_NAME, COLUMN_ID, COLUMN_COMMENT, COLUMN_NAME, COLUMN_TYPE) values ('表A', 'TABLE_A', 2, '单位名称', 'DWMC', 'VARCHAR2(100)');
insert into Z_TABLE_INFO (DEPT_NAME, TABLE_NAME, COLUMN_ID, COLUMN_COMMENT, COLUMN_NAME, COLUMN_TYPE) values ('表A', 'TABLE_A', 3, '招聘岗位', 'ZPGW', 'VARCHAR2(100)');
insert into Z_TABLE_INFO (DEPT_NAME, TABLE_NAME, COLUMN_ID, COLUMN_COMMENT, COLUMN_NAME, COLUMN_TYPE) values ('表A', 'TABLE_A', 4, '薪金待遇', 'XZDY', 'VARCHAR2(100)');
insert into Z_TABLE_INFO (DEPT_NAME, TABLE_NAME, COLUMN_ID, COLUMN_COMMENT, COLUMN_NAME, COLUMN_TYPE) values ('表B', 'TABLE_B', 1, '序号', 'XH', 'NUMBER');
insert into Z_TABLE_INFO (DEPT_NAME, TABLE_NAME, COLUMN_ID, COLUMN_COMMENT, COLUMN_NAME, COLUMN_TYPE) values ('表B', 'TABLE_B', 2, '姓名', 'XM', 'VARCHAR2(100)');
insert into Z_TABLE_INFO (DEPT_NAME, TABLE_NAME, COLUMN_ID, COLUMN_COMMENT, COLUMN_NAME, COLUMN_TYPE) values ('表B', 'TABLE_B', 3, '性别', 'XB', 'NUMBER');
创建测试表:Z_TABLE_DDLSQL,存储建表脚本。
create table Z_TABLE_DDLSQL
(
table_name VARCHAR2(30),
sql_table CLOB,
sql_comment CLOB
);
-- 通过LISTAGG(XMLAGG)实现:
CREATE OR REPLACE PROCEDURE sp_get_ddlsql1(pi_table_name IN VARCHAR2) IS
BEGIN
IF TRIM(pi_table_name) IS NULL THEN
--EXECUTE IMMEDIATE 'TRUNCATE TABLE Z_TABLE_DDLSQL';
DELETE FROM z_table_ddlsql;
ELSE
DELETE FROM z_table_ddlsql WHERE table_name = pi_table_name;
END IF;
INSERT INTO z_table_ddlsql
(table_name
,sql_table
,sql_comment)
SELECT table_name
,'CREATE TABLE ' || table_name || chr(13) || chr(10) || '(' ||
chr(13) || chr(10) ||
listagg(' ' || column_name || ' ' || column_type
,',' || chr(13) || chr(10)) within GROUP(ORDER BY column_id) || chr(13) || chr(10) || ');' AS sql_table
,
/*'COMMENT ON TABLE ' || TABLE_NAME || ' IS ''' || MAX(DEPT_NAME) ||
''';' || CHR(13) || CHR(10) ||
REPLACE(LISTAGG('_##' || COLUMN_NAME || ' IS ''' ||
COLUMN_COMMENT || ''';',
CHR(13) || CHR(10)) WITHIN
GROUP(ORDER BY COLUMN_ID),
'_##',
'COMMENT ON COLUMN ' || TABLE_NAME || '.') AS SQL_COMMENT*/'COMMENT ON TABLE ' ||
table_name ||
' IS ''' ||
MAX(dept_name) ||
''';' ||
chr(13) || xmlagg(xmlparse(content'COMMENT ON COLUMN ' || table_name ||'.' || column_name ||' IS ''' || column_comment ||''';' || chr(13) wellformed) ORDER BY column_id).getclobval() AS column_comment
FROM z_table_info
WHERE 1 = (CASE
WHEN pi_table_name IS NULL THEN
1
ELSE
0
END)
OR table_name = pi_table_name
GROUP BY table_name;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
dbms_output.put_line(SQLERRM);
END sp_get_ddlsql1;
-- 通过游标循环实现
CREATE OR REPLACE PROCEDURE sp_get_ddlsql(pi_table_name IN VARCHAR2) IS
v_sql_table CLOB;
v_sql_comment CLOB;
BEGIN
IF TRIM(pi_table_name) IS NULL THEN
--EXECUTE IMMEDIATE 'TRUNCATE TABLE Z_TABLE_DDLSQL';
DELETE FROM z_table_ddlsql;
ELSE
DELETE FROM z_table_ddlsql WHERE table_name = pi_table_name;
END IF;
FOR c IN (SELECT table_name
,dept_name
,column_name
,column_type
,column_comment
,column_id
,CASE
WHEN column_id = MIN(column_id)
over(PARTITION BY table_name) THEN
1
ELSE
0
END AS fmin
,CASE
WHEN column_id = MAX(column_id)
over(PARTITION BY table_name) THEN
1
ELSE
0
END AS fmax
FROM z_table_info
WHERE 1 = (CASE
WHEN pi_table_name IS NULL THEN
1
ELSE
0
END)
OR table_name = pi_table_name
ORDER BY table_name
,column_id) LOOP
--第一个字段
IF c.fmin = 1 THEN
v_sql_table := 'CREATE TABLE ' || c.table_name || chr(13) ||
chr(10) || '(';
v_sql_comment := 'COMMENT ON TABLE ' || c.table_name || ' IS ''' ||
c.dept_name || ''';';
END IF;
v_sql_table := v_sql_table || chr(13) || chr(10) || ' ' || c.column_name || ' ' ||
c.column_type || CASE
WHEN c.fmax = 0 THEN
','
END;
v_sql_comment := v_sql_comment || chr(13) || chr(10) ||
'COMMENT ON COLUMN ' || c.table_name || '.' ||
c.column_name || ' IS ''' || c.column_comment || ''';';
--最后一个字段
IF c.fmax = 1 THEN
v_sql_table := v_sql_table || chr(13) || chr(10) || ');';
dbms_output.put_line(v_sql_table);
dbms_output.put_line(v_sql_comment);
INSERT INTO z_table_ddlsql
(table_name
,sql_table
,sql_comment)
VALUES
(c.table_name
,v_sql_table
,v_sql_comment);
END IF;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
dbms_output.put_line(SQLERRM);
END sp_get_ddlsql;
CALL sp_get_ddlsql1('TABLE_B');
SELECT * FROM Z_TABLE_DDLSQL;
CALL sp_get_ddlsql('TABLE_A');
SELECT * FROM Z_TABLE_DDLSQL;