【问题标题】:Update a table with dynamic query using bulk collect使用批量收集使用动态查询更新表
【发布时间】:2019-11-28 15:25:33
【问题描述】:

我想使用动态查询、游标和批量收集来更新表。但我不知道语法:

declare
    my_cursor   ?;
    -- other objects;
begin
    execute immediate
        "select s.col1, s.col2, rowid, d.rowid 
        from source_table s, destination_table d
        where s.id = d.id "
    BULK COLLECT INTO my_cursor;

    FORALL i IN my_cursor.FIRST..my_cursor.LAST
        UPDATE destination_table set col_a=my_cursor(i).col1 , col_b=my_cursor(i).col2 
        WHERE rowid = my_cursor(i).rowid;

    commit;
end;

什么是正确的语法和 oracle 对象 请帮忙。

【问题讨论】:

    标签: oracle bulkupdate dynamic-queries


    【解决方案1】:

    你可以这样使用:

    declare
      type REC_TYPE is record (
        ID SOURCE_TABLE.ID%type,
        COL1 SOURCE_TABLE.COL1%type,
        COL2 SOURCE_TABLE.COL2%type
      );
      type REC_CURSOR is ref cursor;
      ref_cursor REC_CURSOR;
      rec REC_TYPE;
      sql_query VARCHAR2(4000);
    begin
      sql_query := 'select s.ID, COL1, COL2 from SOURCE_TABLE s, DESTINATION_TABLE d where s.ID = d.ID';
    
      open ref_cursor for sql_query;
    
      loop
        fetch ref_cursor into rec;
        exit when ref_cursor%NOTFOUND;
    
        update DESTINATION_TABLE
        set COL_A = rec.COL1, COL_B = rec.COL2 
        WHERE ID = rec.ID;
      end loop;
    
      close ref_cursor;
      commit;
    end;
    /
    

    或批量收集:

    declare
      type REC_TYPE is record (
        ID SOURCE_TABLE.ID%type,
        COL1 SOURCE_TABLE.COL1%type,
        COL2 SOURCE_TABLE.COL2%type
      );
      type REC_TYPES is table of REC_TYPE;
      type REC_CURSOR is ref cursor;
      ref_cursor REC_CURSOR;
      recs REC_TYPES;
    
      sql_query VARCHAR2(4000);
    begin
      sql_query := 'select s.ID, COL1, COL2 from SOURCE_TABLE s, DESTINATION_TABLE d where s.ID = d.ID';
    
      open ref_cursor for sql_query;
      fetch ref_cursor bulk collect into recs;
      close ref_cursor;
    
      FOR ind IN recs.FIRST .. recs.LAST
      loop
        update DESTINATION_TABLE
        set COL_A = recs(ind).COL1, COL_B = recs(ind).COL2 
        WHERE ID = recs(ind).ID;
      end loop;
    
      commit;
    end;
    /
    

    【讨论】:

      猜你喜欢
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 2016-08-12
      • 2015-04-24
      • 2013-12-07
      • 2013-08-07
      相关资源
      最近更新 更多