Print an indexed element from the array. : Table of Char « PL SQL « Oracle PL / SQL






Print an indexed element from the array.

    

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','May','June','July','August','September','October','November','December');
  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 [May] is [5]
Index [June] is [6]
Index [July] is [7]
Index [August] is [8]
Index [September] is [9]
Index [October] is [10]
Index [November] is [11]
Index [December] is [12]
Index [April] is [4]
Index [August] is [8]
Index [December] is [12]
Index [February] is [2]
Index [January] is [1]
Index [July] is [7]
Index [June] is [6]
Index [March] is [3]
Index [May] is [5]
Index [November] is [11]
Index [October] is [10]
Index [September] is [9]

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.Check if next index value exists.