2014-12-05 19 views
0

其實我試圖更新1000條記錄,並且我正在爲每5000條記錄進行提交。但我得到了上面的錯誤,因爲我對varray有限制。請爲數組數據類型提供替代方案,以便將100k個鍵值存儲爲數組!代碼示例如下。ORA-06532:超出限制的下標

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; 
/

回答

0

將v_initial初始化爲1而不是0. Varray索引從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. 
... 

哦,別忘了加入錯誤處理,如果進行更新或提交失敗?