0

实际上,我正在尝试更新 1000 条记录,并且我正在为每 5000 条记录进行提交。但由于我对 varray 的限制,我得到了上述错误。请提供数组数据类型的替代方案,甚至将 100k 键值存储为数组!下面的代码示例。

   DECLARE
v_initial number(6):=0;
v_final number(6):=5000;
 -- TOtal number of records V_COUNT 
type lnum IS VARRAY(1000) OF INTEGER; -- change total number of records
iid lnum;
v_maxnum number(8):=0;

BEGIN

iid:=lnum(); -- here 1000 values will be given 

v_maxnum := iid.count;
FOR i in v_initial..v_maxnum LOOP

    UPDATE table SET status='E' WHERE pkey=iid(i);

    IF i=v_final THEN
        COMMIT;
        v_initial:=v_final+1;
        v_final:=v_final+5000;
END IF;
IF i=v_maxnum THEN
         commit;
END IF;

    EXIT WHEN i= v_maxnum;


END LOOP;


END;
/
4

1 回答 1

0

将 v_initial 初始化为 1 而不是 0。可变数组索引从 1 开始。

此外,为了简单起见,这是另一种提交每个 X 记录的技术:

commit_count_const CONSTANT number := 5000; -- commit every 5000
v_loop_counter := 0     
...
FOR i in...
  UPDATE...

  v_loop_counter := v_loop_counter + 1;     -- Increment counter

  -- If the remainder of the counter divided by commit_count_const is 0,
  --  then we hit a multiple of the commit_count so commit.
  IF MOD(v_loop_counter, commit_count_const) = 0 THEN
    COMMIT;
  end if;
...
END LOOP;

COMMIT;  -- commit to catch the rows updated since the last commit.
...

哦,别忘了添加错误处理,如果 UPDATE 或 COMMIT 失败怎么办?

于 2014-12-09T21:47:11.290 回答