Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
37
Добавлен:
16.04.2013
Размер:
5.97 Mб
Скачать

Declaring PL/SQL Collection Variables

DROP TYPE CourseList;

The identifier courses represents an entire nested table. Each element of courses stores the name of a college course such as 'Math 1020'.

Example 5–2 Creating a Table with a Varray Column

The script below creates a database column that stores varrays. Each varray element contains a VARCHAR2.

--Each project has a 16-character code name.

--We will store up to 50 projects at a time in a database column. CREATE TYPE ProjectList AS VARRAY(50) OF VARCHAR2(16);

/

CREATE TABLE department ( -- create database table dept_id NUMBER(2),

name VARCHAR2(15), budget NUMBER(11,2),

--Each department can have up to 50 projects. projects ProjectList);

DROP TABLE department;

DROP TYPE ProjectList;

Declaring PL/SQL Collection Variables

After defining a collection type, you declare variables of that type. You use the new type name in the declaration, the same as with predefined types such as NUMBER.

Example 5–3 Declaring Nested Tables, Varrays, and Associative Arrays

DECLARE

TYPE nested_type IS TABLE OF VARCHAR2(20); TYPE varray_type IS VARRAY(5) OF INTEGER;

TYPE assoc_array_num_type IS TABLE OF NUMBER INDEX BY PLS_INTEGER;

TYPE assoc_array_str_type IS TABLE OF VARCHAR2(32) INDEX BY PLS_INTEGER; TYPE assoc_array_str_type2 IS TABLE OF VARCHAR2(32) INDEX BY VARCHAR2(64); v1 nested_type;

v2 varray_type;

v3 assoc_array_num_type;

v4 assoc_array_str_type;

v5 assoc_array_str_type2; BEGIN

v1 := nested_type('Arbitrary','number','of','strings');

v2 := varray_type(10, 20, 40, 80, 160); -- Up to 5 integers v3(99) := 10; -- Just start assigning to elements.

v3(7) := 100; -- Subscripts can be any integer values. v4(42) := 'Cat'; -- Just start assigning to elements. v4(54) := 'Hat'; -- Subscripts can be any integer values.

v5('Canada')

:=

'North America'; --

Just start

assigning to elements.

v5('Greece')

:=

'Europe';

--

Subscripts

can be string values.

END;

/

5-8 PL/SQL User's Guide and Reference

Declaring PL/SQL Collection Variables

Example 5–4 Declaring Collections with %TYPE

You can use %TYPE to specify the datatype of a previously declared collection, so that changing the definition of the collection automatically updates other variables that depend on the number of elements or the element type:

DECLARE

TYPE Few_Colors IS VARRAY(10) OF VARCHAR2(20); TYPE Many_Colors IS VARRAY(100) OF VARCHAR2(64); some_colors Few_Colors;

--If we change the type of SOME_COLORS from FEW_COLORS to MANY_COLORS,

--RAINBOW and CRAYONS will use the same type when this block is recompiled. rainbow some_colors%TYPE;

crayons some_colors%TYPE;

BEGIN NULL;

END;

/

Example 5–5 Declaring a Procedure Parameter as a Nested Table

You can declare collections as the formal parameters of functions and procedures. That way, you can pass collections to stored subprograms and from one subprogram to another. The following example declares a nested table as a parameter of a packaged procedure:

CREATE PACKAGE personnel AS

TYPE Staff_List IS TABLE OF employees.employee_id%TYPE; PROCEDURE award_bonuses (who_gets_em IN Staff_List);

END personnel;

/

DROP PACKAGE 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.

Example 5–6 Specifying Collection Element Types with %TYPE and %ROWTYPE

To specify the element type, you can use %TYPE, which provides the datatype of a variable or database column. Also, you can use %ROWTYPE, which provides the rowtype of a cursor or database table. Two examples follow:

DECLARE

--Nested table type that can hold an arbitrary number of

--employee IDs. The element type is based on a column from

--the EMPLOYEES table. We do not need to know whether the

--ID is a number or a string.

TYPE EmpList IS TABLE OF employees.employee_id%TYPE;

--Array type that can hold information about 10 employees.

--The element type is a record that contains all the same

--fields as the EMPLOYEES table.

TYPE Top_Salespeople IS VARRAY(10) OF employees%ROWTYPE;

--Declare a cursor to select a subset of columns.

CURSOR c1 IS SELECT first_name, last_name FROM employees;

--Array type that can hold a list of names. The element type

--is a record that contains the same fields as the cursor

Using PL/SQL Collections and Records 5-9

Соседние файлы в папке Oracle 10g