Check if next index value exists. : Table of Char « PL SQL « Oracle PL / SQL






Check if next index value exists.

    
SQL>
SQL> DECLARE
  2    current VARCHAR2(9 CHAR);
  3    element INTEGER;
  4
  5    TYPE months_varray IS VARRAY(12) OF STRING(9 CHAR);
  6    TYPE calendar_table IS TABLE OF VARCHAR2(9 CHAR) INDEX BY VARCHAR2(9 CHAR);
  7
  8    month MONTHS_VARRAY := months_varray('January','February','March','April');
  9
 10    calendar CALENDAR_TABLE;
 11  BEGIN
 12    IF calendar.COUNT = 0 THEN
 13      FOR i IN month.FIRST..month.LAST LOOP
 14        calendar(month(i)) := TO_CHAR(i);
 15        DBMS_OUTPUT.PUT_LINE('Index ['||month(i)||'] is ['||i||']');
 16      END LOOP;
 17
 18      FOR i IN 1..calendar.COUNT LOOP
 19        IF i = 1 THEN
 20          current := calendar.FIRST;
 21          element := calendar(current);
 22        ELSE
 23          IF calendar.NEXT(current) IS NOT NULL THEN
 24            current := calendar.NEXT(current);
 25            element := calendar(current);
 26          ELSE
 27            EXIT;
 28          END IF;
 29        END IF;
 30
 31        DBMS_OUTPUT.PUT_LINE('Index ['||current||'] is ['||element||']');
 32      END LOOP;
 33    END IF;
 34  END;
 35  /
Index [January] is [1]
Index [February] is [2]
Index [March] is [3]
Index [April] is [4]
Index [April] is [4]
Index [February] is [2]
Index [January] is [1]
Index [March] is [3]

PL/SQL procedure successfully completed.

   
    
    
    
  








Related examples in the same category

1.Use table of char as an array
2.FIRST, LAST, NEXT, and PRIOR.
3.Loop backwards over the table
4.Print an indexed element from the array.