在这种特殊情况下,分裂离目标还有很长的路要走。
考虑到一个目标表可能有很多列(是的,在一个不同的变量中处理每个列需要5个),我建议使用schema dictionary来增加一些灵活性。
让我们看一个接受两个参数的过程:表名和包含逗号分隔值列表的字符串。
这里假设表只有字符串、数字和时间列。要实现完整的版本,请在过程的开头添加对所有必需数据类型的处理。
select level as column_id,
REGEXP_SUBSTR(pi_values_list, '[^,]+', 1, level) as column_val
from dual connect by REGEXP_SUBSTR(pi_values_list, '[^,]+', 1, level) is not null;
整个过程如下:
create or replace procedure myInsertInto(pi_table_name char,
pi_values_list char)
is
v_statement varchar2(30000) := 'INSERT INTO %TABLE_NAME% (%COLUMNS_LIST%) VALUES (%VALUES_LIST%)';
v_columns_list varchar2(10000);
v_values_list varchar2(10000);
begin
SELECT LISTAGG(T.column_name, ',') within group (order by T.column_id) ,
LISTAGG( -- implement specific types handling here
CASE
WHEN S.column_val IS NULL
THEN 'NULL'
WHEN T.data_type = 'NUMBER'
THEN S.column_val
WHEN T.data_type IN ('DATE', 'TIMESTAMP')
THEN 'TIMESTAMP ''' || S.column_val || ''''
WHEN T.data_type like '%CHAR%'
THEN '''' || S.column_val || ''''
ELSE 'NULL'
END,
',') within group (order by T.column_id)
into v_columns_list,
v_values_list
from user_tab_cols T,
(select level as column_id, REGEXP_SUBSTR(pi_values_list, '[^,]+', 1, level) as column_val
from dual connect by REGEXP_SUBSTR(pi_values_list, '[^,]+', 1, level) is not null) S
where T.table_name = pi_table_name
and T.column_id = S.column_id;
if v_columns_list IS NULL then
raise_application_error(-20000, 'Not found columns for table ' || pi_table_name);
end if;
-- finalizing the statement
v_statement := replace(v_statement, '%TABLE_NAME%', pi_table_name);
v_statement := replace(v_statement, '%COLUMNS_LIST%', v_columns_list);
v_statement := replace(v_statement, '%VALUES_LIST%', v_values_list);
execute immediate v_statement;
end;
/
那就这样用吧
create table MY_TABLE (
col_a VARCHAR2(10),
col_b NUMBER,
col_c VARCHAR2(10),
col_d DATE,
col_E VARCHAR2(10) default 'DEFAULT'
);
begin
myInsertInto('MY_TABLE', 'abc,123,xyz,2018-01-02 23:01:10,pqr' );
myInsertInto('MY_TABLE', 'def,345,mkr' );
myInsertInto('MY_TABLE', 'fgh' );
end;
/