2014-11-05 14 views
0

我已經創建了自定義Oracle記錄類型,我試圖填充一些值並嘗試訪問另一個存儲過程中的相同內容(實際上我將從Java - 我也在Java中得到相同的錯誤),但我得到了「超出計數的下標」錯誤消息。無法將數據添加到Oracle自定義記錄 - 錯誤 - 超出計數的下標

我只是一個初學者存儲過程。 以下是我所做的。

Oracle客戶記錄類型

create or replace type learnerMapCustomRecord as object(activityName varchar2(20),activityDescn varchar2(20)); 

create or replace type learnerMapCustomRecordTable as table of learnerMapCustomRecord; 

存儲過程來填充值,記錄類型

create or replace PROCEDURE getLearnerMapDetails(learnerMapCustomRecord out learnerMapCustomRecordTable) as 
cursor c1 is select object_name,status from user_objects where rownum <= 2; 
c c1%rowtype; 
i number:=1; 
begin 
    learnerMapCustomRecord := learnerMapCustomRecordTable(); 
    open c1; 
    loop 
    fetch c1 into c; 
    EXIT WHEN C1%NOTFOUND; 
    dbms_output.put_line(c.object_name||'==>'||c.status); 
    -- learnerMapCustomRecord.extend; 
    learnerMapCustomRecord(I).activityName:=C.OBJECT_NAME; 
    learnerMapCustomRecord(i).activityDescn:=c.status; 
    i:=i+1; 
    end loop; 
end; 

從那裏我調用上面的SP訪問自定義記錄類型的列表存儲過程。

create or replace procedure data_collection_extract as 
learnerMapCustomRecord learnerMapCustomRecordTable; 
begin 
    getLearnerMapDetails(learnerMapCustomRecord); 
    for i in learnerMapCustomRecord.first..learnerMapCustomRecord.last 
    LOOP 
    dbms_output.put_line(learnerMapCustomRecord(i).activityName||'==>'||learnerMapCustomRecord(i).activityDescn); 
    end loop; 
end; 

set serveroutput on; 
exec data_collection_extract(); 
/
show error; 
Error report - 
ORA-06533: Subscript beyond count 
ORA-06512: at "FOL_DEV.GETLEARNERMAPDETAILS", line 13 
ORA-06512: at "FOL_DEV.DATA_COLLECTION_EXTRACT", line 4 
ORA-06512: at line 1 
06533. 00000 - "Subscript beyond count" 
*Cause: An in-limit subscript was greater than the count of a varray 
      or too large for a nested table. 
*Action: Check the program logic and explicitly extend if necessary. 

你能解釋我做錯了什麼嗎?

+0

你的'CURSOR'返回[超過2147483647行](http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/collections.htm#LNPLS00512)?因爲它是嵌套表可容納的最大值。 – 2014-11-05 18:28:52

回答

0

幾個問題:

  1. 你是不是延長您的收藏(儘管該.extend()方法是存在的......註釋掉)。
  2. 你沒有實例化你的對象類learnerMapCustomRecord
  3. 你沒有關閉你的光標c1
  4. 您的輸出參數與您的類名稱相同,因爲缺省標識符作用域解析有效地使對象類的實例化更加困難。
  5. 通過逐行使用fetch,您可以在SQL引擎上進行太多的往返操作(對於自己也有太多工作),您可以在其中執行常規光標for環回或批量提取。

解決方案:

create or replace PROCEDURE getLearnerMapDetails(theOutput out learnerMapCustomRecordTable) 
as 
    cursor c1 is select object_name,status from user_objects where rownum <= 2; 
    c c1%rowtype; 
begin 
    theOutput := learnerMapCustomRecordTable(); 
    open c1; 
    loop 
    fetch c1 into c; 
    EXIT WHEN C1%NOTFOUND; 
    dbms_output.put_line(c.object_name||'==>'||c.status); 
    theOutput.extend(); 
    theOutput(theOutput.last) := new learnerMapCustomRecord(
     activityName => c.object_name, 
     activityDescn => c.status 
    ); 
    end loop; 
    close c1; 
end; 

...並與SQL engnie的往返減少...

create or replace PROCEDURE getLearnerMapDetails(theOutput out learnerMapCustomRecordTable) 
as 
    cursor c1 is select object_name,status from user_objects where rownum <= 2; 
begin 
    theOutput := learnerMapCustomRecordTable(); 
    for c in c1 loop 
    dbms_output.put_line(c.object_name||'==>'||c.status); 
    theOutput.extend(); 
    theOutput(theOutput.last) := new learnerMapCustomRecord(
     activityName => c.object_name, 
     activityDescn => c.status 
    ); 
    end loop; 
end; 

...如果你想這樣做一個「硬漢方式「,那麼你可以這樣做...

create or replace PROCEDURE getLearnerMapDetails(theOutput out learnerMapCustomRecordTable) as 
begin 
    select new learnerMapCustomRecord(
     activityName => object_name, 
     activityDescn => status 
    ) 
    bulk collect into theOutput 
    from user_objects 
    where rownum <= 2; 
end; 

享受。

相關問題