2012-03-22 17 views
5

¿Es posible ejecutar un bucle para cada bucle en una matriz PL/SQL?Cómo escribir un ciclo FOR FOREACH en PL/SQL?

+0

Evite construcciones de bucle en SQL. Comience a pensar en operaciones basadas en SET. http://www.simple-talk.com/sql/database-administration/the-road-to-professional-database-development-set-based-thinking/ – Oded

+0

La documentación de Oracle es completa, en línea y gratuita. Debes aprender a usarlo para responder preguntas de sintaxis triviales. Aquí está la sección de los bucles PL/SQL. http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/controlstructures.htm#i8296 – APC

+0

Leer la documentación también habría respondido a su pregunta siguiente http://stackoverflow.com/q/9827581/146325 también – APC

Respuesta

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

Aquí hay otro ejemplo: http://stackoverflow.com/a/7012775/402322 – ceving

0

No es posible iterar sobre las matrices asociativas con un índice no numérico con un bucle FOR. La solución anterior funciona bien.

-- 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; 
Cuestiones relacionadas