PL/SQL ARRAY
The PL/SQL programming language provides a data structure called the VARRAY, which can store a
fixed-size sequential collection of elements of the same type. A varray is used to store an ordered
collection of data, however it is often better to think of an array as a collection of variables of the same
type.
All varrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.
An array is a part of collection type data and it stands for variable-size arrays.
SYNTAX: −
TYPE varray_type_name IS VARRAY(n) of <element_type>
For example −
TYPE namearray IS VARRAY(5) OF VARCHAR2(10);
Type grades IS VARRAY(5) OF INTEGER;
Example:
SQL> set serveroutput on
SQL> DECLARE Output:
type namesarray IS VARRAY(5) OF
VARCHAR2(10); Total 5 Students
type grades IS VARRAY(5) OF INTEGER; Student: Kavita
names namesarray; Marks: 98
marks grades; Student: Pritam
total integer; Marks: 97
BEGIN Student: Ayan
names := namesarray('Kavita', 'Pritam', 'Ayan', Marks: 78
'Rishav', 'Aziz'); Student: Rishav
marks:= grades(98, 97, 78, 87, 92); Marks: 87
total := names.count; Student: Aziz
dbms_output.put_line('Total '|| total || ' Marks: 92
Students');
FOR i in 1 .. total LOOP PL/SQL procedure successfully completed.
dbms_output.put_line('Student: ' || names(i) ||
' SQL>
Marks: ' || marks(i));
END LOOP;
END;
/