Varrays: Varrays Stands For Variable-Size Array
Varrays: Varrays Stands For Variable-Size Array
PL/SQL Example1:-
[sql]DECLARE
/* define a varray with 5 rows */
TYPE char_type IS VARRAY (5) OF VARCHAR2 (10);
/*calls the Varray constructor */
c_list char_type := char_type ();
BEGIN /* allocates space in the varray */
c_list.EXTEND;
c_list (1) := ‘SCOTT’;
/* Can have up to 10 characters */
c_list.EXTEND;
c_list (2) := ‘TIGER’;
/* print the varray elements by using its methods FIRST and LAST */
FOR i IN c_list.FIRST.. c_list.LAST
LOOP
DBMS_OUTPUT.put_line (c_list (i));
END LOOP;
END;[/sql]
Output:-
Example2:-
[sql]DECLARE
/*define a varray of “emp” type*/
TYPE myarray IS VARRAY (20) OF emp%ROWTYPE; /*declare and initialize a null set of rows*/
v_list myarray := myarray ();
CURSOR cu_emp IS SELECT * FROM emp;
v_index NUMBER;
BEGIN v_index := 1;
FOR i IN cu_emp LOOP /*initialize row*/
v_list.EXTEND;
SELECT * INTO v_list (v_index) FROM emp
WHERE empno = i.empno; /*print the contents*/
DBMS_OUTPUT.put_line (’empno = ‘|| v_list (v_index).empno|| ‘ ename =’|| v_list
(v_index).ename);
/*increment the index value*/
v_index := v_index+ 1;
END LOOP;
END; [/sql]
Output:–
/* DBMS OUtput is */
Common Exceptions in Varrays:-
Example3:-
[sql]DECLARE
/* define a varray with 5 rows */
TYPE char_type IS VARRAY (5) OF VARCHAR2 (10);
/*we declare a varray but not initialize so system will raise an error*/
c_list char_type;
BEGIN
/* allocates space in the varray */
c_list.EXTEND;
c_list (1):= ‘SCOTT’;
/* Can have up to 10 characters */
c_list.EXTEND;
c_list (2) := ‘TIGER’;
/* print the varray elements by using its methods FIRST and LAST */
FOR i IN c_list.FIRST.. c_list.LAST
LOOP
DBMS_OUTPUT.put_line (c_list (i));
END LOOP;
END;[/sql]
Output:
ERROR Report:
ORA-06531: Reference to uninitialized collection
ORA-06512: at line 8
This exception raises when you forgot to initialize the varray. So once after declaration of the varray
variable and then initialize the variable is must otherwise system will rise above exception.
Example4:-
[sql]DECLARE
/* define a varray with 5 rows */
TYPE char_type IS VARRAY (3) OF VARCHAR2 (10);
c_list char_type := char_type ();
BEGIN
c_list.EXTEND;
c_list (1):= ‘SCOTT’;
/* Can have up to 10 characters */
c_list.EXTEND;
c_list (2):= ‘TIGER’;
/* before allocates space in the varray we try to use that variable then system will raise an error
*/
c_list (3):= ‘KING’;
/* print the varray elements by using its methods FIRST and LAST */
FOR i IN c_list.FIRST.. c_list.LAST
LOOP
DBMS_OUTPUT.put_line (c_list (i));
END LOOP;
END;[/sql]
Output:-
Error report:
ORA-06533: Subscript beyond count
ORA-06512: at line 12
The exception means that subscript 3 is unavailable. It does not exist. While you defined the varray
as three elements in size, you allocate the space for only two elements. Therefore the variable has
only two valid subscripts, one and two.
Example5:-
[sql]DECLARE
/* define a varray with 5 rows */
TYPE char_type IS VARRAY (3) OF VARCHAR2 (10);
c_list char_type := char_type ();
BEGIN
/* allocates space in the varray */
c_list.EXTEND;
c_list (1):= ‘SCOTT’;
/* Can have up to 10 characters */
c_list.EXTEND;
c_list (2) := ‘TIGER’;
c_list.EXTEND;
c_list (3) := ‘KING’;
c_list.EXTEND;
/* print the varray elements by using its methods FIRST and LAST */
FOR i IN c_list.FIRST.. c_list.LAST
LOOP
DBMS_OUTPUT.put_line (c_list (i));
END LOOP;
END;[/sql]
Output:-
Error report:
ORA-06532: Subscript outside of limit
ORA-06512: at line 14
In the above program we declare the varray size is 3 then we try to allocate the space for the 4th
element then system will raise an error.