【问题标题】:How to insert data from one table to other table by other mapping table through procedure如何通过过程通过其他映射表将数据从一个表插入到另一个表
【发布时间】:2013-11-29 15:53:33
【问题描述】:

有两个表

Table1(Column11, Column12, Column13)
Table2(Column21, Column22)

还有如下的映射表:

Table3(Sourcetable,Source column ,Destination table ,Destination column)

如何通过过程将 Table1 中的数据作为源表和目标表作为 Table2 插入?

我正在使用 oracle 11g;请帮助实现这一目标!

感谢和问候, 杀手

【问题讨论】:

  • 你试过什么?解决方案可以使用游标和动态 sql 语句(例如 EXECUTE IMMEDIATE)。

标签: oracle oracle11g


【解决方案1】:

试试这个:

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

【讨论】:

  • 嗨,韦尼弗里德!你能帮忙解决新的要求吗-
  • 如果需求更改为以下,那么解决方案是什么-=?有4个表: Table1(Column11, Column12, Column13) Table2(Column21, Column22) Table3(Column31, Column32,column33) Table4(Column21, Column22) 以及以下映射表: Table5(Sourcetable,Source column ,Destination table ,Destination column) 如何将Table1中的数据插入为源表,目标表为Table2,table3,table,4, Through Procedures?
猜你喜欢
  • 2023-03-17
  • 2019-07-18
  • 1970-01-01
  • 2017-03-30
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
  • 2014-08-10
  • 1970-01-01
相关资源
最近更新 更多