0% found this document useful (0 votes)
27 views

PL SQL ArrayReference

The document discusses PL/SQL arrays called varrays, which allow storing a fixed number of elements of the same type in a sequential collection. Varrays use contiguous memory locations with the first element at the lowest address and last at highest. Each element has an index and varrays can be dynamically resized. A varray type is created using CREATE TYPE, specifying maximum size and element type. Examples demonstrate declaring a varray type, populating and iterating over a varray, and passing a varray to a procedure.

Uploaded by

Zin Min Htet
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

PL SQL ArrayReference

The document discusses PL/SQL arrays called varrays, which allow storing a fixed number of elements of the same type in a sequential collection. Varrays use contiguous memory locations with the first element at the lowest address and last at highest. Each element has an index and varrays can be dynamically resized. A varray type is created using CREATE TYPE, specifying maximum size and element type. Examples demonstrate declaring a varray type, populating and iterating over a varray, and passing a varray to a procedure.

Uploaded by

Zin Min Htet
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PL/SQL

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.

Creating a Varray Type


A varray type is created with the CREATE TYPE statement. You must specify the maximum
size and the type of elements stored in the varray.

The basic syntax for creating a VARRAY type at the schema level is

CREATE OR REPLACE TYPE varray_type_name IS VARRAY(n) of <element_type>

Where,

varray_type_name is a valid attribute name,

n is the number of elements (maximum) in the varray,

element_type is the data type of the elements of the array.


1.

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

SQL> CREATE OR REPLACE PROCEDURE testing (t_in MyType) IS


2 BEGIN
3 FOR i IN 1..t_in.count LOOP
4 dbms_output.put_line(t_in(i));
5 END LOOP;
6 END;
7 /

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 /

You might also like