WHILE Loop: Test the loop counter before each loop iteration to insure that it is still less than 50 : While Loop « PL SQL « Oracle PL / SQL






WHILE Loop: Test the loop counter before each loop iteration to insure that it is still less than 50

    
SQL>
SQL>
SQL> CREATE TABLE MyTable (
  2    num_col    NUMBER,
  3    char_col   VARCHAR2(60)
  4    );

Table created.

SQL>
SQL>
SQL> DECLARE
  2    v_Counter BINARY_INTEGER := 1;
  3  BEGIN
  4    WHILE v_Counter <= 50 LOOP
  5      INSERT INTO MyTable
  6        VALUES (v_Counter, 'Loop index');
  7      v_Counter := v_Counter + 1;
  8    END LOOP;
  9  END;
 10  /

PL/SQL procedure successfully completed.

SQL>
SQL> select * from MyTable;

   NUM_COL CHAR_COL
---------- ------------------------------------------------------------
         1 Loop index
         2 Loop index
         3 Loop index
         4 Loop index
         5 Loop index
         6 Loop index
         7 Loop index
         8 Loop index
         9 Loop index
        10 Loop index
        11 Loop index

   NUM_COL CHAR_COL
---------- ------------------------------------------------------------
        12 Loop index
        13 Loop index
        14 Loop index
        15 Loop index
        16 Loop index
        17 Loop index
        18 Loop index
        19 Loop index
        20 Loop index
        21 Loop index
        22 Loop index

   NUM_COL CHAR_COL
---------- ------------------------------------------------------------
        23 Loop index
        24 Loop index
        25 Loop index
        26 Loop index
        27 Loop index
        28 Loop index
        29 Loop index
        30 Loop index
        31 Loop index
        32 Loop index
        33 Loop index

   NUM_COL CHAR_COL
---------- ------------------------------------------------------------
        34 Loop index
        35 Loop index
        36 Loop index
        37 Loop index
        38 Loop index
        39 Loop index
        40 Loop index
        41 Loop index
        42 Loop index
        43 Loop index
        44 Loop index

   NUM_COL CHAR_COL
---------- ------------------------------------------------------------
        45 Loop index
        46 Loop index
        47 Loop index
        48 Loop index
        49 Loop index
        50 Loop index

50 rows selected.

SQL>
SQL> drop table MyTable;

Table dropped.

SQL>

   
    
    
  








Related examples in the same category

1.Put two statement in while loop
2.Example of a WHILE loop that never executes
3.Corrected WHILE loop that executes
4.While Loop with condition
5.Change while loop counter
6.While loop with complex conditions
7.Insert value in while loop
8.Do calculation with while loop counter
9.Call EXIT to jump out of a while loop
10.Use EXIT WHEN to exit a while loop
11.WHILE..LOOP, Cursor Loop
12.WHILE Loop: This condition will evaluate to NULL, since v_Counter is initialized to NULL by default
13.WHILE Loop
14.This script demonstrates variable visibility
15.The WHILE loop uses a loop index value as its gate on entry criterion:
16.While loop and number counter