Samedi 23 juillet 2011 6 23 /07 /Juil /2011 01:13

 

PLSQL FOR - IN - LOOP.

Comment utiliser FOR IN en PLSQL. ?

 

Structures répétitives PLSQL.

Dans un traitement itératif la structure FOR IN permet de boucler grâce à un nombre d'itérations connu à l'avance.
Le corps de la boucle est exécuté une fois pour chaque entier dans la plage définie par LOWER BOUND .. UPPER BOUND.
Par défaut, après chaque itération, l'indice de la boucle LOWER BOUND est incrémenté à la hausse sauf si vous utilisez le mot-clé REVERSE, alors l'indice de la boucle UPPER BOUND est décrémenté.

Voici la principale forme itérative avec l'instruction FOR.

  • Traitement Itératif FOR - IN - LOOP - END LOOP.

Traitement Itératif FOR IN.

 

Syntaxe FOR - IN - LOOP - END LOOP.

 
FOR compteur IN [REVERSE] lower_bound..upper_bound LOOP
    instruction1;
    instruction2;
    instruction3;
    [EXIT WHEN condition];
END LOOP;

 
SQL> SET SERVEROUTPUT ON
SQL> BEGIN
  2    FOR i IN 1..5 LOOP
  3      CASE i
  4         WHEN 1 THEN DBMS_OUTPUT.PUT_LINE ('--               ');
  5         WHEN 2 THEN DBMS_OUTPUT.PUT_LINE ('-----            ');
  6         WHEN 3 THEN DBMS_OUTPUT.PUT_LINE ('---------        ');
  7         WHEN 4 THEN DBMS_OUTPUT.PUT_LINE ('--------------   ');
  8         WHEN 5 THEN DBMS_OUTPUT.PUT_LINE ('-----------------');
  9      END CASE;
 10    END LOOP;
 11  END;
 12  /
--
-----
---------
--------------
-----------------

Procédure PL/SQL terminée avec succès.

SQL>
 
SQL> SET SERVEROUTPUT ON
SQL> BEGIN
  2    FOR i IN REVERSE 1..5 LOOP
  3      CASE i
  4         WHEN 1 THEN DBMS_OUTPUT.PUT_LINE ('--               ');
  5         WHEN 2 THEN DBMS_OUTPUT.PUT_LINE ('-----            ');
  6         WHEN 3 THEN DBMS_OUTPUT.PUT_LINE ('---------        ');
  7         WHEN 4 THEN DBMS_OUTPUT.PUT_LINE ('--------------   ');
  8         WHEN 5 THEN DBMS_OUTPUT.PUT_LINE ('-----------------');
  9      END CASE;
 10    END LOOP;
 11  END;
 12  /
-----------------
--------------
---------
-----
--

Procédure PL/SQL terminée avec succès.

SQL>
 
SQL> SET SERVEROUTPUT ON
SQL> DECLARE
  2      employe scott.emp%rowtype;
  3  CURSOR Mon_Curseur IS
  4      SELECT ename, deptno
  5        FROM emp
  6       WHERE deptno = 10;
  7  BEGIN
  8       FOR employe IN Mon_Curseur LOOP
  9         dbms_output.put_line(' Nom : '||employe.ename);
 10       END LOOP;
 11  END;
 12  /
Nom : CLARK
Nom : KING
Nom : MILLER

Procédure PL/SQL terminée avec succès.

SQL>

 

Haut de Page www.dba-ora.fr

Liens en rapport avec l'article et qui sont susceptibles de vous intéresser
• PL-SQL IF
• PL-SQL CASE
• PL-SQL FOR
Par Daniel Roesch - Publié dans : ORACLE PLSQL
Ecrire un commentaire - Voir les 0 commentaires
Retour à l'accueil

Rechercher Articles Oracle SQL sur ce Site.

Loading

À lire aussi…

Download PDF Oracle

Rechercher sur le site

Oracle 10G - DBA -

Syndication

  • Flux RSS des articles
Contact - C.G.U. - Rémunération en droits d'auteur - Signaler un abus - Articles les plus commentés