Collection
Collection
DECLARE
happyfamily list_of_names_t;
l_row PLS_INTEGER;
BEGIN
l_row := happyfamily.FIRST;
END;
Using a nested table
In the following example, I first declare a nested table type as a schema-level type. In my
PL/SQL block, I declare three nested tables based on that type. I put the names of everyone in
my family into the happyfamily nested table. I put the names of my children in the children
nested table. I then use the set operator, MULTISET EXCEPT (introduced in Oracle
Database 10g), to extract just the parents from the happyfamily nested table; finally, I display
the names of the parents. A more thorough explanation appears in the table after the code:
BEGIN
happyfamily.EXTEND (4);
happyfamily (1) := 'Eli';
happyfamily (2) := 'Steven';
happyfamily (3) := 'Chris';
happyfamily (4) := 'Veva';
children.EXTEND;
children (1) := 'Chris';
children.EXTEND;
children (2) := 'Eli';
If your PL/SQL application requires negative subscripts, you also have to use
associative arrays.
you’d find it useful to perform high-level set operations on your collections, choose
nested tables over associative arrays.
If you want to preserve the order of elements stored in the collection column and if
your dataset will be small, use a VARRAY.
Atomically null;
Atomically null;
Empty (cannot be null); illegal to
Uninitialized state illegal to reference
elements undefined reference
elements
elements
BINARY_INTEGER
Positive integer Positive integer
(and any of its
Index type between 1 and between 1 and
subtypes) or
2,147,483,647 2,147,483,647
VARCHAR2
Characteristic Associative array Nested table VARRAY
Use built-in
EXTEND
EXTEND (or
procedure (or
Means of Assign value to element TRIM), but only
TRIM to
extending with a new subscript up to declared
condense), with no
maximum size
predefined
maximum
Yes, Oracle
Can be compared
No Database 10g and No
for equality?
later
Retains ordering
and subscripts
when stored in and N/A No Yes
retrieved from
database?
Method
(function or Description
procedure)
DELETE Removes one or more elements from the collection. Reduces COUNT
procedure if the element is not already removed. With VARRAYs, you can
Method
(function or Description
procedure)
FIRST, LAST
Returns the smallest (FIRST) and largest (LAST) subscript in use.
functions
Exception
If COUNT is applied to an uninitialized nested table or a VARRAY, it raises the
COLLECTION_IS_NULL predefined exception. Note that this exception is not
possible for associative arrays, which do not require initialization.
Delete Method - When applied to VARRAYs, you can issue DELETE only without
arguments (i.e., remove all rows). In other words, you cannot delete individual rows
of a VARRAY, possibly making it sparse. The only way to remove a row from a
VARRAY is to TRIM from the end of the collection
If DELETE is applied to an uninitialized nested table or a VARRAY, it raises the
COLLECTION_ IS_NULL predefined exception
If EXTEND is applied to an uninitialized nested table or a VARRAY, it raises the
COLLECTION_IS_NULL predefined exception. An attempt to EXTEND a
VARRAY beyond its declared limit raises the SUBSCRIPT_BEYOND_LIMIT
exception
If FIRST and LAST are applied to an uninitialized nested table or a VARRAY, they
raise the COLLECTION_ IS_NULL predefined exception.
The PRIOR and NEXT Methods
PRIOR returns the next-lower index value in use relative to i; NEXT returns the next higher
TYPE bunch_of_pets_t
IS
TABLE OF pet_t INDEX BY PLS_INTEGER;
my_pets bunch_of_pets_t;
BEGIN
my_pets (1) :=
pet_t (
100
, 'Mercury'
, vet_visits_t (vet_visit_t ('01-Jan-2001', 'Clip wings')
, vet_visit_t ('01-Apr-2002', 'Check cholesterol')
)
);
DBMS_OUTPUT.put_line (my_pets (1).name);
DBMS_OUTPUT.put_line
(my_pets(1).petcare.LAST).reason);
DBMS_OUTPUT.put_line (my_pets.COUNT);
DBMS_OUTPUT.put_line (my_pets (1).petcare.LAST);
END;
MULTISET Example
CREATE TABLE birds (
genus VARCHAR2(128),
species VARCHAR2(128),
colors color_tab_t,
PRIMARY KEY (genus, species)
);
DECLARE
happyfamily aa_pkg.strings_t;
BEGIN
happyfamily (1) := 'Me';
happyfamily (2) := 'You';
happyfamily strings_t;
BEGIN
happyfamily (1) := 'Me';
happyfamily (2) := 'You';
Return
Operation Description
value
TABLE
y [NOT]
SUBMULTISET
[OF] x
DECLARE
my_cursor SYS_REFCURSOR;
Declaring Cursor Variables
cursor_name cursor_type_name;
Opening Cursor Variables
OPEN cursor_name FOR select_statement;
Fetching from Cursor Variables
FETCH cursor_variable_name INTO record_name;
FETCH cursor_variable_name INTO variable_name, variable_name ...;
Compile-time rowtype matching rules
These are the rules that PL/SQL follows at compile time:
Two cursor variables (including procedure parameters) are compatible for assignments
and argument passing if any of the following are true:
o Both variables (or parameters) are of a strong REF CURSOR type with the
same rowtype_name.
o Both variables (or parameters) are of a weak REF CURSOR type, regardless of
the rowtype_name.
o One variable (or parameter) is of any strong REF CURSOR type, and the other is of
any weak REF CURSOR type.
A cursor variable (or parameter) of a strong REF CURSOR type may be OPEN FOR a query
that returns a rowtype that is structurally equal to the rowtype_name in the original type
declaration.
A cursor variable (or parameter) of a weak REF CURSOR type may be OPEN FOR any
query. The FETCH from such a variable is allowed INTO any list of variables or record structure.