PL SQL ArrayReference
PL SQL ArrayReference
Array Examples
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. We will study
other collection types in a later chapter 'PL/SQL Collections'.
Each element in a varray has an index associated with it. It also has a maximum size that can
be changed dynamically.
The basic syntax for creating a VARRAY type at the schema level is
Where,
declare
type array_t is varray(3) of varchar2(10);
array array_t := array_t('Matt', 'Joanne', 'Robert');
begin
for i in 1..array.count loop
dbms_output.put_line(array(i));
end loop;
end;
2.
DECLARE
CURSOR c_customers is
SELECT name FROM customers;
type c_list is varray (6) of customerS.No.ame%type;
name_list c_list := c_list();
counter integer :=0;
BEGIN
FOR n IN c_customers LOOP
counter := counter + 1;
name_list.extend;
name_list(counter) := n.name;
dbms_output.put_line('Customer('||counter ||'):'||
name_list(counter));
END LOOP;
END;
/
3.
SQL> set serveroutput on
SQL> CREATE OR REPLACE TYPE MyType AS VARRAY(200) OF VARCHAR2(50);
2 /
Type created
Procedure created
SQL> DECLARE
2 v_t MyType;
3 BEGIN
4 v_t := MyType();
5 v_t.EXTEND(10);
6 v_t(1) := 'this is a test';
7 v_t(2) := 'A second test line';
8 testing(v_t);
9 END;
10 /