2014-09-25 75 views
0

我想在PL/SQL中做一些非常基本的事情,但我一直在擁有...如何將項目推入數組並遍歷它們?如何將項目推入數組並在PL/SQL中遍歷它們?

谷歌搜索似乎暗示使用owa_text.multi_line;

owa_text.multi_line就是這種類型的記錄:

/* A multi_line is just an abstract datatype which can hold */ 
    /* large amounts of text data as one piece.     */ 
    type multi_line is record 
    (
     rows  vc_arr, 
     num_rows integer, 
     partial_row boolean 
    ); 

要通過vc_arr迭代,我們必須使用l_array.first.. l_array.last。但是在嘗試訪問它時出現錯誤。

下面是一個簡單的示例發現負載不同的值到一個數組:

declare 
    l_persons owa_text.multi_line := owa_text.new_multi(); 
/* Documentation of owa_text.new_multi(): Standard "make element" routines. */ 
--function new_multi return multi_line; 

    l_value_exists boolean := false; 

    cursor c_get_orders is 
    select person, 
      choice 
    from my_orders;   

    begin 
      for i in c_get_orders loop 
       l_value_exists := false; 
       for j in l_persons.rows.first.. l_persons.rows.last loop --Fails here, 
    --PL/SQL: numeric or value error 
          if l_persons.rows(j) = i.person then 
           l_value_exists := true; 
           exit; 
          end if; 
        end loop; 
        if not l_value_exists then 
          owa_text.add2multi(i.person, l_persons); 
        end if; 
       end loop; 
       for i in l_persons.rows.first.. l_persons.rows.last loop 
        write_to_log(l_persons.rows(i)); 
       end loop; 
     end; 

我缺少什麼?我該怎麼做呢?

編輯:這是一個腳本才能完成設置,如果它可以幫助效仿的榜樣:

create table my_orders 
(
    person varchar2(4000 byte), 
    choice varchar2(4000 byte) 
); 

insert into my_orders 
    (person, choice) 
values 
    ('Tom', 'Juice'); 
insert into my_orders 
    (person, choice) 
values 
    ('Jane', 'Apple'); 
insert into my_orders 
    (person, choice) 
values 
    ('Tom', 'Cake'); 
insert into my_orders 
    (person, choice) 
values 
    ('Jane', 'Chocolate'); 
insert into my_orders 
    (person, choice) 
values 
    ('Tom', 'Coffee'); 
commit; 
+0

謝謝,我修復了這個問題。但我仍然得到同樣的錯誤。有任何想法嗎? – Zesty 2014-09-25 12:34:25

+1

new_multi()方法的功能是什麼? – Aramillo 2014-09-25 12:38:41

+0

這是一個函數,用於初始化multi_line/*標準「make元素」例程。 */ 函數new_multi返回multi_line; – Zesty 2014-09-25 12:39:47

回答

2

推測,該new_multi()方法初始化一個空採集。

一個甲骨文藏品更深奧的特點是,使用FIRST/LAST遍歷空集不工作 - 你必須要麼檢查集合是否爲空,或者使用1 .. <collection>.COUNT代替:

declare 
    type t_number_nt is table of number; 
    l_numbers t_number_nt := t_number_nt(); 
begin 
    -- raises ORA-006502 
    /* for i in l_numbers.first .. l_numbers.last 
    loop 
    dbms_output.put_line(l_numbers(i)); 
    end loop; 
    */ 
    -- doesn't raise an error 
    for i in 1 .. l_numbers.count loop 
    dbms_output.put_line(l_numbers(i)); 
    end loop; 
end; 

UPDATE

對於技術遍歷PL/SQL集合了更詳盡的解釋,請參閱本OTN article by Steven Feuerstein

1

它是這樣的:

declare 
    l_persons owa_text.multi_line; 
begin 
    OWA_TEXT.new_multi (l_persons); 
    FOR i IN 1 .. l_persons.num_rows 
    loop 
    null; 
    end loop; 
end; 
相關問題