如何在 PL/SQL 中编写 FOR EACH 循环?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9826913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:57:30  来源:igfitidea点击:

How to write a FOR EACH loop in PL/SQL?

sqlplsql

提问by antonpug

Is it possible to run a for each loop on a PL/SQL array?

是否可以在 PL/SQL 数组上运行 for each 循环?

回答by A.B.Cade

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

回答by 0xdb

It's no possible to iterate over the associative arrays with a non numeric index with a FOR-loop. The solution below works just fine.

不可能使用 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;