2012-03-22 42 views
5

是否可以在PL/SQL數組上運行每個循環?如何在PL/SQL中編寫FOR EACH循環?

+0

避免在SQL中循環構造。開始思考基於SET的操作。 http://www.simple-talk.com/sql/database-administration/the-road-to-professional-database-development-set-based-thinking/ – Oded 2012-03-22 16:57:11

+0

Oracle文檔全面,在線且免費。你應該學會如何使用它來爲你自己回答一些微不足道的語法問題。這是關於PL/SQL循環的部分。 http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/controlstructures.htm#i8296 – APC 2012-03-22 17:51:10

+0

讀取文件建立也已經回答了你的問題,以後也http://stackoverflow.com/q/9827581/146325 – APC 2012-03-22 17:53:07

回答

10
for i in my_array.first ..my_array.last loop 
    --do_something with my_array(i); 
end loop; 
+0

這裏是另一個例子:http://stackoverflow.com/a/7012775/402322 – ceving 2014-10-20 16:06:22

0

這是沒有可能用一個非數字索引的關聯數組遍歷一個for循環。上面的解決方案工作得很好。

-- for-each key in (associative-array) loop ... 
declare 
    type items_type is table of varchar2(32) index by varchar2(32); 
    items items_type; 
begin 
    items('10') := 'item 10'; 
    items('20') := 'item 20'; 
    items('30') := 'item 30'; 
    dbms_output.put_line('items=' || items.count); 

    <<for_each>> declare key varchar2(32); begin loop 
     key := case when key is null then items.first else items.next(key) end; 
     exit when key is null; 
     dbms_output.put_line('item(' || key || ')=' || items(key)); 
     --do something with an item 
    end loop; end for_each; 
end;