0% found this document useful (0 votes)
146 views5 pages

Varrays: Varrays Stands For Variable-Size Array

Varrays are variable-sized arrays in Oracle that have a fixed maximum size. Elements are inserted starting from index 1 up to the maximum declared size with no gaps. Varrays can be used in both SQL and PL/SQL and allow accessing elements individually or as a whole collection. Examples demonstrate how to declare, initialize, extend, and access varray elements. Common exceptions like referencing an uninitialized varray or using an invalid subscript beyond the declared size are also shown.

Uploaded by

Bhagwat Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views5 pages

Varrays: Varrays Stands For Variable-Size Array

Varrays are variable-sized arrays in Oracle that have a fixed maximum size. Elements are inserted starting from index 1 up to the maximum declared size with no gaps. Varrays can be used in both SQL and PL/SQL and allow accessing elements individually or as a whole collection. Examples demonstrate how to declare, initialize, extend, and access varray elements. Common exceptions like referencing an uninitialized varray or using an invalid subscript beyond the declared size are also shown.

Uploaded by

Bhagwat Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

VARRAYS

 Varrays stands for variable-size array.


 A Varrays has a fixed limit on its size, specified as part of the declaration. Elements are
inserted into varray starting at index 1, up to maximum length declared in the varray type.
 Data in varray can be either manipulated as a whole or individually as elements.
 Varrays cannot have any gaps in the index sequence. Index always starts from 1 not 0;
 We would use a varray whenever the physical size of the collection is static. These are like
the traditional arrays in other programming languages.
 We can use Varrays in SQL as well as PL/SQL.

 
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:-

anonymous block completed /* Script Output */

SCOTT  /*DBMS Output *?


TIGER

 
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:–

anonymous block completed  /Script Output  */

/* DBMS OUtput is */

empno = 7839 ename =KING


empno = 7698 ename =BLAKE
empno = 7782 ename =CLARK
empno = 7499 ename =ALLEN
empno = 7521 ename =WARD
empno = 7654 ename =MARTIN
empno = 7844 ename =TURNER
empno = 7900 ename =JAMES
empno = 7934 ename =MILLER
/* the output might be different depending upon the availability of rows and updates
*/

 
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.

You might also like