Initialization and assignment with a unique string index value to an associative array. : varray « PL SQL « Oracle PL / SQL






Initialization and assignment with a unique string index value to an associative array.

   
SQL>
SQL> SET ECHO ON
SQL> SET SERVEROUTPUT ON SIZE 1000000
SQL>
SQL> DECLARE
  2
  3    current VARCHAR2(9 CHAR);
  4    element INTEGER;
  5
  6    TYPE months_varray IS VARRAY(12) OF STRING(9 CHAR);
  7
  8    TYPE calendar_table IS TABLE OF VARCHAR2(9 CHAR)INDEX BY VARCHAR2(9 CHAR);
  9
 10    month MONTHS_VARRAY := months_varray('January','February','March','April','May','June','July','August');
 11
 12    calendar CALENDAR_TABLE;
 13
 14  BEGIN
 15
 16    IF calendar.COUNT = 0 THEN
 17
 18      FOR i IN month.FIRST..month.LAST LOOP
 19
 20        calendar(month(i)) := TO_CHAR(i);
 21
 22        DBMS_OUTPUT.PUT_LINE('Index :'||month(i)||' is '||i);
 23
 24      END LOOP;
 25      FOR i IN 1..calendar.COUNT LOOP
 26
 27        IF i = 1 THEN
 28
 29          current := calendar.FIRST;
 30
 31          element := calendar(current);
 32
 33        ELSE
 34
 35          IF calendar.NEXT(current) IS NOT NULL THEN
 36
 37            current := calendar.NEXT(current);
 38
 39            element := calendar(current);
 40
 41          ELSE
 42
 43            EXIT;
 44
 45          END IF;
 46
 47        END IF;
 48
 49        DBMS_OUTPUT.PUT_LINE('Index :'||current||' is '||element);
 50
 51      END LOOP;
 52
 53    END IF;
 54
 55  END;
 56  /
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 :April is 4
Index :August is 8
Index :February is 2
Index :January is 1
Index :July is 7
Index :June is 6
Index :March is 3
Index :May is 5

PL/SQL procedure successfully completed.

SQL>

   
    
    
  








Related examples in the same category

1.Varray constructors.
2.legal and illegal varray assignments.
3.TYPE Strings IS VARRAY(5) OF VARCHAR2(10)
4.Create a varray based on user defined type
5.Store pre-defined constants in VARRAY
6.Creating and Using VARRAYs
7.Query a stored varray.
8.VARRAY of VARCHAR2 and Varray of number
9.Table of numbers and varray of numbers
10.Initialize the array and create two entries using the constructor
11.Reference elements in varray
12.assignments to varray elements, and the ORA-6532 and ORA-6533 errors.
13.ORA-06533: Subscript beyond count
14.Compare two varray variables
15.Constructs a two varrays and one nested table type in the database
16.Control varray index with if statement
17.Create type prices with a varray of number
18.Declare the varray with null values.
19.Declaring a VARRAY of scalar variable
20.Define a varray of integer with 3 rows
21.Define a varray of twelve strings.
22.Define a varray with a null element constructor and extends it one element at a time by a formula
23.Define a varray with a null element constructor and extends it one element at a time.
24.Define a varray with a three element constructor of null elements and attempt to populate it beyond three elements.
25.Define a varray with a three element constructor of null elements.
26.Hard code value in varray and use for loop to insert them to a table
27.Nested varray
28.Reference varray.count in for loop
29.Store 12 months in varray of string
30.Use table() function to display varray type column
31.exceeded maximum VARRAY limit
32.Assign values to subscripted members of the varray.
33.Check the size of a varray
34.Declare an array initialized as a no-element collection.
35.Extend with null element to the maximum limit size.
36.Initialization and assignment with a numeric index value to an associative array.
37.Associative array example
38.Assigns a value to the indexed value.
39.Avoid traversing an associative array where no elements are initialized.
40.Subscript index values begin at 1, not 0