0

我的 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);   
4

1 回答 1

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))也相同。

有很多关于数组处理的资料,但PSOUG和文档一样很有帮助。


从您的评论来看,您可能明确引用了该集合,即something := t_listofterms(7). 这假定存在特定数量的行。Bulk collect填充从1n的集合,其中n是查询返回的行数。如果您想用它做某事而不是显式引用,那么循环遍历它通常会更好。就像是,

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

    do_something;

end loop;
于 2012-04-11T20:42:20.183 回答