有两个表
Table1(Column11, Column12, Column13)
Table2(Column21, Column22)
以下映射表:
Table3(Sourcetable,Source column ,Destination table ,Destination column)
如何将Table1中的数据作为源表和目标表插入Table2,Through Procedures?
我正在使用oracle 11g;请帮助实现这一目标!
谢谢&问候, 杀手
答案 0 :(得分:0)
试试这个:
DECLARE
sqlstr varchar2(1000);
SourceCol varchar2(1000);
DestCol varchar2(1000);
BEGIN
FOR aTab IN (SELECT DISTINCT Source_table, Destination_table FROM Table3) LOOP
FOR aCol IN (SELECT Source_column, Destination_column
FROM Table3
WHERE Source_table = aTab.Source_table
AND Destination_table = aTab.Destination_table)
LOOP
SourceCol := SourceCol || aCol.Source_column ||',';
DestCol := DestCol || aCol.Destination_column ||',';
END LOOP;
SourceCol := REGEXP_REPLACE(SourceCol, ',$');
DestCol := REGEXP_REPLACE(DestCol, ',$');
sqlstr := 'insert into '||aTab.Destination_table||' ('||DestCol
||') SELECT '||SourceCol||' FROM '||aTab.Source_table;
EXECUTE IMMEDIATE sqlstr;
END LOOP;
END;
/
您可以在此处找到示例架构SQL Fiddle