ORA-06532:超出限制错误的下标

时间:2012-04-11 20:20:27

标签: sql arrays plsql

我的pl / sql过程中出现ORA-06532错误。它与我的数组有关,它似乎正在开始的行上发生:“term_1:= ......”。选择的查询获得7条记录,因此它应该与我要求的数量相同。我对SQL数组有点新意,所以我可能错过了一些明显的东西。感谢任何帮助。谢谢。

DECLARE

listOfTerms      VC50_ARRAY;
term_1           varchar2(30);
term_2           varchar2(30);
term_3           varchar2(30);
term_4           varchar2(30);
term_5           varchar2(30);
term_6           varchar2(30);
term_7           varchar2(30);

BEGIN

SELECT apl.awdp_acad_terms 
BULK COLLECT INTO listOfTerms
FROM fa_years fay 
   JOIN award_periods_ls apl
     ON apl.award_periods_id = fay.award_periods_id 
WHERE (SELECT b.awdp_fa_year as faYear 
      FROM award_periods_ls a 
           JOIN coll18_test.fa_years b ON a.award_periods_id = b.award_periods_id 
     WHERE awdp_acad_terms = v_ug_term) = fay.awdp_fa_year
ORDER BY apl.awdp_acad_terms DESC;

term_1   := listOFTerms(1);     
term_2   := listOFTerms(2);             
term_3   := listOFTerms(3);
term_4   := listOFTerms(4);             
term_5   := listOFTerms(5);             
term_6   := listOFTerms(6);             
term_7   := listOFTerms(7);   

1 个答案:

答案 0 :(得分:1)

我认为你过于复杂了。您的代码相当于:

DECLARE

   -- Declare the cursor explicitly.
   cursor c_my_cursor is
     SELECT apl.awdp_acad_terms 
       FROM fa_years fay 
       JOIN award_periods_ls apl
         ON apl.award_periods_id = fay.award_periods_id 
      WHERE ( SELECT b.awdp_fa_year as faYear 
                FROM award_periods_ls a 
                JOIN coll18_test.fa_years b 
                  ON a.award_periods_id = b.award_periods_id 
               WHERE awdp_acad_terms = v_ug_term ) = fay.awdp_fa_year
      ORDER BY apl.awdp_acad_terms DESC;

   -- Create a-user defined type that is the same as a single row in the cursor.
   type t__listOfTerms is table of c_my_cursor%rowtype index by binary_integer;
   -- Initialise a variable that is of data-type t__listofterms.
   t_listofterms t__listofterms;

BEGIN

   open c_my_cursor;
   fetch c_my_cursor bulk collect into t_listofterms;
   close c_my_cursor;

END;

然后,您可以按索引值引用类型中的项目,因此term_1t_listofterms(1)相同。不需要创建具有相同值的附加变量;您可以采用相同的方式引用它,因此length(term1)length(t_listofterms(1))也是相同的。

关于数组处理有很多内容,但PSOUGthe documentation一样有用。


根据您的评论判断,您可能会明确地引用该集合,即something := t_listofterms(7)。这假定存在特定数量的行。 Bulk collect 1 中的集合填充到 n ,其中 n 是查询返回的行数。如果你想用它做一些事情而不是明确的引用,通常最好循环使用它。像,

for i in t_listofterms.first .. t_listofterms.last loop

    do_something;

end loop;