Oracle PL/SQL 从数组批量更新

Oracle PL/SQL BULK Update from an arrray

是否可以像这样在 Oracle 中执行批量更新:

TYPE type_A IS RECORD
(
    var    NUMBER(15,2)
);

TYPE t_type_A IS VARRAY (9) OF type_A; 
var_t_type_A  t_type_A;


SELECT
    A
BULK COLLECT INTO var_t_type_A
FROM
    B;
    
-- lopp over

FOR i IN var_t_type_A.FIRST .. var_t_type_A.LAST
LOOP
    var_t_type_A.var = 99;
END LOOP;
    

和 insert 类似,这样做

   FORALL var_i_idx IN 1..table_of_t_created_new_buchung.count SAVE EXCEPTIONS
       UPDATE TABLE B somehow

我不知道是否可以通过更新来完成,但是全局临时 table 而不是数组类型呢?

类似于:

create global temporary table temp_table (xid rowid, value number(15, 2));

insert into temp_table select rowid, value from main_table;

update temp_table ....

update (select m.value v1, t.value v2
          from main_table m 
           join temp_table t on m.rowid = t.xid)
   set v1 = v2

好吧,您的框架代码相当稀疏,所以任何答案都是必要的。首先,是的,你看到想要的是可能的。其次,您的代码中有几个问题:

  1. 没有理由为 a 创建包含单个的记录 变量。
  2. 假设 varray 可以与批量收集/forall 一起使用。我有 只是从来没有用过一个,从来没有看到要点。
  3. 您的 forall 语句包含“保存异常”子句,但是 没有例外部分,这是无用的。我已经包括了 必要的定义。
    declare 
        bulk_errors  EXCEPTION;  
        PRAGMA EXCEPTION_INIT (bulk_errors , -24381);  
        
        type t_type_a is table of number(15,2);  
        var_t_type_a  t_type_a;
    
    begin
    
    
        select a
          bulk collect 
          into var_t_type_a
          from b;
        
        -- lopp over
    
        for i in var_t_type_a.first .. var_t_type_a.last
        loop
            var_t_type_a(i) = 99;
        end loop;
        
        ...
    
    
        forall var_i_idx in 1..var_t_type_a.count save exceptions
           update table b 
              set some_column = var_t_type_a(var_i_indx); 
              
    exception
        when bulk_errors then 
             <logic for bulk errors>;
    end ;

更完整的答案post更完整的问题描述。