PL SQL Collections
PL SQL Collections
Overview of Collections
A collection is an ordered group of elements, all of the same
type
Each element has a unique subscript that determines its
position in the collection
Although collections can have only one dimension, you can
model multi-dimensional arrays by creating collections whose
elements are also collections
To use collections in an application, you define one or more
PL/SQL types, then define variables of those types
Collection Types
PL/SQL offers these collection types:
Index-by tables, also known as associative arrays, let you look
up elements using arbitrary numbers and strings for subscript
values
DECLARE
TYPE nested_type IS TABLE OF VARCHAR2(20);
TYPE varray_type IS VARRAY(50) OF INTEGER;
TYPE associative_array_type IS TABLE OF NUMBER
INDEXED BY BINARY_INTEGER;
v1 nested_type;
v2 varray_type;
v3 associative_array_type;
p1 v2%TYPE; -- %TYPE declaration
Declaring PL/SQL Collection
Variables
Example: Declaring a Procedure Parameter as a Nested Table
CREATE PACKAGE personnel AS
TYPE Staff IS TABLE OF Employee;
...
PROCEDURE award_bonuses (members IN Staff);
END personnel;
To call PERSONNEL.AWARD_BONUSES from outside the package,
you declare a variable of type PERSONNEL.STAFF and pass that
variable as the parameter.
You can also specify a collection type in the RETURN clause of a
function specification:
DECLARE
TYPE SalesForce IS VARRAY(25) OF Salesperson;
FUNCTION top_performers (n INTEGER) RETURN SalesForce IS .....
Declaring PL/SQL Collection
Variables
Example: Specifying Collection Element Types with %TYPE and %ROWTYPE
DECLARE
TYPE EmpList IS TABLE OF emp.ename%TYPE; -- based on column
CURSOR c1 IS SELECT * FROM dept;
TYPE DeptFile IS VARRAY(20) OF c1%ROWTYPE; -- based on cursor
Example: VARRAY of Records
DECLARE
TYPE AnEntry IS RECORD (
term VARCHAR2(20),
meaning VARCHAR2(200));
TYPE Glossary IS VARRAY(250) OF AnEntry;
Example: NOT NULL Constraint on Collection Elements
DECLARE
TYPE EmpList IS TABLE OF emp.empno%TYPE NOT NULL;
Initializing Collections
Until you initialize it, a nested table or varray is atomically null: the
collection itself is null, not its elements.
You must explicitly call a constructor for each varray and nested table
variable.
collection_name(subscript)
DECLARE
TYPE Roster IS TABLE OF VARCHAR2(15);
names Roster := Roster('J Hamil', 'D Caruso', 'R Singh');
j BINARY_INTEGER := 2;
BEGIN
FOR i IN names.FIRST .. names.LAST
LOOP
IF names(i) = 'J Hamil' THEN -- Referencing by Subscript
NULL;
END IF;
END LOOP;
verify_name(names(j)); -- Passing as Parameter
END;
Assigning Collections
You can assign the value of an expression to a specific element
in a collection using the syntax:
collection_name(subscript) := expression;
where expression yields a value of the type specified for
elements in the collection type definition
DECLARE
english_courses CourseList;
BEGIN
SELECT courses INTO english_courses FROM department
WHERE name = 'English';
END;
Using PL/SQL Collections with SQL
Statements.........
Example: Updating a Nested Table within a Database Table
You can revise the list of courses offered by the English Department:
DECLARE
new_courses CourseList :=
CourseList('Expository Writing',
'Film and Literature',
'Discursive Writing',
'Modern English Grammar',
'Realism and Naturalism',
'The Short Story',
'The American Novel',);
BEGIN
UPDATE department
SET courses = new_courses WHERE name = 'English';
END;
Manipulating Individual Collection
Elements with SQL
BEGIN
-- The TABLE operator makes the statement apply to the nested
-- table from the 'History' row of the DEPARTMENT table.
INSERT INTO
TABLE(SELECT courses FROM department WHERE name =
'History')
VALUES('Modern China');
END;
Manipulating Individual Collection
Elements with SQL..........
Example: Retrieving a Single Element from a Nested Table with SQL
In the following example, you retrieve the title of a specific
course offered by the History Department:
DECLARE
my_title VARCHAR2(64);
BEGIN
-- We know that there is one history course with 'Etruscan'
-- in the title. This query retrieves the complete title
-- from the nested table of courses for the History department.
SELECT title INTO my_title
FROM
TABLE(SELECT courses FROM department WHERE name = 'History')
WHERE name LIKE '%Etruscan%';
END;
Manipulating Individual Collection
Elements with SQL..........
Example: Updating Elements Inside a Nested Table with SQL
In this example, you abbreviate the names for some courses offered by
the Psychology Department:
BEGIN
UPDATE TABLE(SELECT courses FROM department
WHERE name = 'Psychology')
SET credits = credits + adjustment
WHERE course_no IN (2200, 3540);
END;
Manipulating Individual Collection
Elements with SQL..........
Example: Deleting Elements from a Nested Table with SQL
In this example, you delete all 5-credit courses offered by the English
Department:
BEGIN
DELETE TABLE(SELECT courses FROM department
WHERE name = 'English')
WHERE credits = 5;
END;
Performing DML Operations on a
Varray with SQL
Currently, you cannot reference the individual elements of
a varray in an INSERT, UPDATE, or DELETE statement
declare
type t2 is table of t1;
v2 t2;
begin
select c1 BULK COLLECT INTO v2 from tab1;
dbms_output.put_line(v2.count); -- prints 2
end;
Collection Methods
A collection method is a built-in function or procedure that operates
on collections and is called using dot notation. The syntax follows:
collection_name.method_name[(parameters)]