40
Using Collections By Michael McLaughlin

Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

  • Upload
    others

  • View
    20

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Using Collections

By Michael McLaughlin

Page 2: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Using Collections – Objectives

• What are collections? • What are Oracle’s collection data types?

– Associative arrays – Variable size arrays – Nested tables

• How do you convert PL/SQL collections to SQL result sets?

• How do you update nested tables and table view columns inside tables?

Page 3: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

What are Collections?

• Collections are groups of elements that share the same data type.

• Arrays – Have a fixed maximum number of elements. – Are densely populated structures, which means

they’re indexed by sequential index values. • List

– Have a unlimited (memory excluded) number of elements.

– Are sparsely populated structures, which means they’re indexed by non-sequential index values.

Page 4: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Collection Types

• PL/SQL – Collections Types – Associative Arrays (Oracle 8i index-by tables) – Variable Size Arrays (VARRAY) – Nested Tables

• SQL – Collection Types – Aggregate Table (Pipelined Table Result) – Variable Size Arrays (VARRAY) – Nested Tables

Page 5: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Associative Arrays

• Indexed by a PLS_INTEGER or String • Index is

– Dense, or sequential numbers – Sparse, or non-sequential numbers or strings

• Initial State – empty • Scope of Use – Only in PL/SQL blocks • Elements – Scalar or record data type

Page 6: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Associative Array – Scalar Data Type DECLARE TYPE scalar_table IS TABLE OF NUMBER INDEX BY PLS_INTEGER; lv_scalar_table SCALAR_TABLE; BEGIN lv_scalar_table(1) := 1; lv_scalar_table(2) := 2; FOR i IN 1..lv_scalar_table.COUNT LOOP dbms_output.put_line( '['||i||']'|| '['||lv_scalar_table(i)||']'); END LOOP; END; /

Collection Library Call

Index phrase unique to the PL/SQL only associative array.

Scalar Data Type

Ordinary parenthesis for subcripts

Page 7: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Associative Array – String Index DECLARE lv_current VARCHAR2(20); TYPE scalar_table IS TABLE OF NUMBER INDEX BY VARCHAR2(20); lv_scalar_table SCALAR_TABLE; BEGIN lv_scalar_table('One') := 1; lv_scalar_table('Two') := 2; lv_current := lv_scalar_table.FIRST; WHILE lv_current IS NOT NULL LOOP dbms_output.put_line( '['||lv_current||']'|| '['||lv_scalar_table(lv_current)||']'); lv_current := lv_scalar_table.next(lv_current); END LOOP; END; /

Set the index starting point.

Index phrase uses String index.

Local variable to traverse a sparsely populated list.

Get the next index as current index.

A while loop lets you navigate a sparsely populated collection.

Page 8: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Associative Array – Record Data Type DECLARE TYPE composite_record IS RECORD ( id NUMBER , element VARCHAR2(60)); TYPE scalar_table IS TABLE OF COMPOSITE_RECORD INDEX BY PLS_INTEGER; lv_scalar_table SCALAR_TABLE; BEGIN lv_scalar_table(1).id := 1; lv_scalar_table(1).element := 'One'; FOR i IN 1..lv_scalar_table.COUNT LOOP dbms_output.put_line( '['||lv_scalar_table(i).id||']'|| '['||lv_scalar_table(i).element||']'); END LOOP; END; /

Assignment to elements of a local variable.

A local record data type, which can be in a package

specification.

Access via variable to element.

Page 9: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Associative Array – Record Data Type

DECLARE

...

lv_counter NUMBER := 1;

CURSOR c IS SELECT item_id, item_title FROM item; BEGIN

FOR i IN c LOOP

lv_scalar_table(lv_counter).id := i.item_id; lv_scalar_table(lv_counter).element := i.item_title; lv_counter := lv_counter + 1;

END LOOP;

...

END;

/

Assignment of cursor columns to elements of a local collection.

A local cursor that queries a result set from a table.

Page 10: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Associative Array – Record Data Type DECLARE TYPE composite_record IS RECORD ( id NUMBER , element VARCHAR2(60)); ... lv_counter NUMBER := 1; CURSOR c IS SELECT item_id, item_title FROM item; BEGIN FOR i IN c LOOP lv_scalar_table(lv_counter) := i; lv_counter := lv_counter + 1; END LOOP; ... END; /

Assignment of a cursor record to a local collection of the

same record data type.

A local cursor that queries a result set from a table.

Define the record type to match the cursor’s structure.

Page 11: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Variable Size Arrays

• Indexed by an integer • Index is always dense (sequentially numbered) • Initial State – Uninitialized • Scope of Use – SQL or PL/SQL blocks • Elements – Scalar or attribute data type • Maximum Element Size – Set when declared

Page 12: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Variable Size Array – Scalar Data Type

DECLARE

TYPE scalar_varray IS VARRAY(5) OF NUMBER; lv_scalar_varray SCALAR_VARRAY :=

scalar_varray(1,2,3,4); BEGIN

FOR i IN 1..lv_scalar_varray.COUNT LOOP dbms_output.put_line('['||lv_scalar_varray(i)||']');

END LOOP;

END;

/

A variable size array can use the COUNT method to find the number of initialized

elements or the LIMIT method to find the maximum number of possible elements.

Initializes a 4 element variable size array.

An array of 5 elements of a scalar data type

Page 13: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Variable Size Array – Scalar Data Type

DECLARE

TYPE scalar_varray IS VARRAY(5) OF NUMBER; lv_scalar_varray SCALAR_VARRAY :=

scalar_varray(1,2,3,4); BEGIN

FOR i IN 1..lv_scalar_varray.LIMIT LOOP dbms_output.put_line('['||lv_scalar_varray(i)||']');

END LOOP;

END;

/

Initializes a 4 element variable size array.

An array of 5 elements of a scalar data type

ORA-06533: Subscript beyond count error is thrown when the variable size array holds fewer than it’s maximum

number of elements.

Page 14: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Variable Size Array – Record Data Type

DECLARE

TYPE composite_record IS RECORD ( id NUMBER , element VARCHAR2(60)); TYPE scalar_varray IS VARRAY(5) OF COMPOSITE_RECORD; lv_scalar_varray SCALAR_VARRAY := scalar_varray(); BEGIN

lv_scalar_varray.EXTEND(2); lv_scalar_varray(1).id := 1;

lv_scalar_varray(1).element := 'One';

lv_scalar_varray(2).id := 2;

lv_scalar_varray(2).element := 'Two';

...

END;

/

Extends memory to store two elements of the record structure in

the collection.

An array of 5 elements of a composite data type

Assigns variables to elements of a variable size array.

Initializes an empty colleciton.

A PL/SQL record type.

Page 15: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Variable Size Array – Record Data Type

DECLARE

...

TYPE scalar_varray IS VARRAY(100) OF COMPOSITE_RECORD;

lv_scalar_varray SCALAR_VARRAY := scalar_varray();

lv_counter NUMBER := 1;

CURSOR c IS SELECT item_id, item_title FROM item;

BEGIN

FOR i IN c LOOP

lv_scalar_varray.EXTEND;

lv_scalar_varray(lv_counter) := i;

lv_counter := lv_counter + 1;

END LOOP;

...

END;

/

Extends memory for each row from the cursor.

Initialize the collection as a null element collection.

Define a 100 element array.

Page 16: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Using Collections - ADT

• Attribute Data Type (ADT) – Is an object type defined in SQL – It has a default constructor that mirrors the

number and data types of local attributes – It can have override constructors

• Collections of ADTs – Are usable as parameters to function and

procedures – Are usable as function return data types

Page 17: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Variable Size Array – Object Data Type

CREATE OR REPLACE

TYPE composite_object IS OBJECT ( id NUMBER , element VARCHAR2(60)); /

CREATE OR REPLACE

TYPE composite_table IS VARRAY(100) OF composite_object; /

Create a SQL schema level object type with a default constructor of a

number and string.

Creates a variable size array of a SQL object type or attribute data type.

Page 18: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Variable Size Array – Object Data Type

DECLARE

lv_scalar_varray SCALAR_VARRAY :=

scalar_varray(composite_object(1,'One') ,composite_object(2,'Two') ,composite_object(3,'Three') ,composite_object(4,'Four') ,composite_object(5,'Five')); BEGIN

FOR i IN 1..lv_scalar_varray.COUNT LOOP

dbms_output.put_line(

'['||lv_scalar_varray(i).id||']'||

'['||lv_scalar_varray(i).element||']');

END LOOP;

END;

/ Access elements of the

nested object type by name.

Initialize the object data type elements through

object construction.

Define a 100 element array.

Page 19: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Variable Size Array – Object Data Type

DECLARE

lv_scalar_varray SCALAR_VARRAY := scalar_varray();

lv_counter NUMBER := 1;

CURSOR c IS SELECT item_id, item_title FROM item;

BEGIN

FOR i IN c LOOP

lv_scalar_varray.EXTEND;

lv_scalar_varray(lv_counter) :=

composite_object(i.item_id, i.item_title); lv_counter := lv_counter + 1;

END LOOP;

...

END;

/

Initialize the object data type elements through

object construction.

Page 20: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Variable Size Array – Object Data Type

CREATE OR REPLACE FUNCTION get_array RETURN scalar_varray IS lv_scalar_varray SCALAR_VARRAY := scalar_varray();

lv_counter NUMBER := 1;

CURSOR c IS SELECT item_id, item_title FROM item;

BEGIN

FOR i IN c LOOP

lv_scalar_varray.EXTEND;

lv_scalar_varray(lv_counter) :=

composite_object(i.item_id, i.item_title);

lv_counter := lv_counter + 1;

END LOOP;

RETURN lv_scalar_varray; END;

/

Return the instantiated and populated collection.

Collection return type.

Page 21: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Variable Size Array – Object Data Type

COLUMN id FORMAT 999999 HEADING "ID #"

COLUMN element FORMAT A60 HEADING "Element Name"

SELECT *

FROM TABLE(get_array);

Or, you can just one element of the composite object, or ADT, like this:

SELECT element

FROM TABLE(get_array);

Return one element of the object structure.

Return all element of the object structure.

Page 22: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Nested Tables – Like a List

• Indexed by an integer • Index begins dense (sequentially numbered)

but may become sparse over use • Initial State – Uninitialized • Scope of Use – SQL or PL/SQL blocks • Elements – Scalar or attribute data type • Maximum Element Size – None

Page 23: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Nested Table – Scalar Data Type DECLARE

TYPE scalar_table IS TABLE OF NUMBER; lv_scalar_table SCALAR_TABLE :=

scalar_table(1,2,3,4); BEGIN

FOR i IN 1..lv_scalar_table.COUNT LOOP dbms_output.put_line('['||lv_scalar_table(i)||']');

END LOOP;

END;

/

A nested table can use the COUNT method to find the number of initialized elements but not the LIMIT method because it has no upward limit like a variable size array.

Initializes a 4 element unbound collection of numbers.

A collection of as many elements as memory allows.

Page 24: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Nested Table – Object Data Type

CREATE OR REPLACE

TYPE composite_object IS OBJECT ( id NUMBER , element VARCHAR2(60)); /

CREATE OR REPLACE

TYPE composite_table IS TABLE OF composite_object; /

Create a SQL schema level object type with a default constructor of a

number and string.

Creates a nested table of a SQL object type or attribute data type.

Page 25: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Nested Table – Object Data Type

DECLARE

lv_scalar_table SCALAR_TABLE := scalar_table();

lv_counter NUMBER := 1;

CURSOR c IS SELECT item_id, item_title FROM item;

BEGIN

FOR i IN c LOOP

lv_scalar_table.EXTEND;

lv_scalar_table(lv_counter) :=

composite_object(i.item_id, i.item_title); lv_counter := lv_counter + 1;

END LOOP;

...

END;

/

Initialize the object data type elements through

object construction.

Page 26: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Nested Table – User-Defined Type

CREATE OR REPLACE TYPE composite_object IS OBJECT

( id NUMBER

, name VARCHAR2(30)

, CONSTRUCTOR FUNCTION composite_object

(pv_name VARCHAR2) RETURN SELF AS RESULT);

/

Default constructor maps to the signature of attributes in the object type.

Returns an instance of the object type as the result of constructing an

instance of the object type.

Page 27: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Nested Table – User-Defined Type

CREATE OR REPLACE TYPE BODY composite_object IS

CONSTRUCTOR FUNCTION composite_object (pv_name VARCHAR2)

RETURN SELF AS RESULT IS

composite_obj COMPOSITE_OBJECT := composite_object(object_id.NEXTVAL,pv_name); BEGIN

SELF := composite_obj; RETURN;

END composite_object;

END;

/

Returns an instance of the object type.

Constructs a local copy using the default constructor.

Page 28: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Nested Table – Object Data Type

DECLARE

lv_scalar_table SCALAR_TABLE := scalar_table();

lv_counter NUMBER := 1;

CURSOR c IS SELECT item_id, item_title FROM item;

BEGIN

FOR i IN c LOOP

lv_scalar_table.EXTEND;

lv_scalar_table(lv_counter) :=

composite_object(i.item_title); lv_counter := lv_counter + 1;

END LOOP;

...

END;

/

Initializes the object with the overloaded single

parameter constructor.

Page 29: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Collection API

• COUNT method returns the number of elements in a collection

• DELETE method removes elements from a collection for: – Associative Arrays – Nested Tables

• EXISTS method checks if a specific element exists in the collection

Page 30: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Collection API (continue)

• EXTEND method allocates space for a new element or set of elements in a collection

• FIRST method finds the first index in a collection

• LAST method finds the last index in a collection

• LIMIT method only works with variable size arrays and returns the maximum number of values that fit in that type of collection

Page 31: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Collection API (continue)

• NEXT method finds the next index in the collection

• PRIOR method finds the previous index in the collection

• TRIM method removes one or more elements from the end of a collection

Page 32: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Convert PL/SQL Collection to SQL

• You use a Pipelined Table Function to convert a PL/SQL variable size array or nested table to an aggregate result set

• You use the TABLE function to unwrap the aggregate result set in a query

• Pipelined Table Functions work with – Scalar return values – Composite return values

Page 33: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Pipelined Table – Shared PL/SQL Types

CREATE OR REPLACE PACKAGE utility_type IS

TYPE common_lookup_record IS RECORD ( common_lookup_id NUMBER , common_lookup_type VARCHAR2(30) , common_lookup_meaning VARCHAR2(255)); TYPE common_lookup_varray IS VARRAY(10) OF common_lookup_record; TYPE common_lookup_table IS TABLE OF common_lookup_record;

TYPE common_lookup_plsql_table IS TABLE OF common_lookup_record INDEX BY BINARY_INTEGER;

END utility_type;

/

Shared collection type.

Shared record type.

Page 34: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Pipelined Table – Shared PL/SQL Types

CREATE OR REPLACE FUNCTION get_common_lookup_sql_varray

( table_name VARCHAR2, column_name VARCHAR2 )

RETURN utility_type.common_lookup_varray PIPELINED IS

collection UTILITY_TYPE.COMMON_LOOKUP_VARRAY;

list UTILITY_TYPE.COMMON_LOOKUP_VARRAY :=

utility_type.common_lookup_varray();

BEGIN

...

FOR i IN 1..collection.COUNT LOOP

list.EXTEND;

list(i) := collection(i);

PIPE ROW(list(i)); END LOOP;

RETURN; END get_common_lookup_sql_varray; Return the piped table.

Put rows in the pipe.

Page 35: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Updating Nested Tables

• Nested tables come in two varieties – Collections of object types – Collections of scalar variables

• Collections of scalar variables are also called table view columns, and they have retrictions in SQL – You can’t change part of them – You must replace the entire collection

Page 36: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Updating Nested Object Tables

UPDATE TABLE (SELECT e.home_address

FROM employee1 e

WHERE e.employee_id = 1) e

SET e.street_address =

street_list('222 Union Street','Suite 522')

, e.city = 'Oakland'

WHERE e.address_id = 1; Update an element of a nested object type by using a

inline view with the TABLE function.

Table View Columns must be completely

replaced.

Page 37: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Updating Table View Columns

CREATE OR REPLACE FUNCTION update_collection ( old_element_collection STREET_LIST , old_element_value VARCHAR2 , new_element_value VARCHAR2 ) RETURN STREET_LIST IS

lv_counter NUMBER := 1; lv_element_collection STREET_LIST := street_list();

BEGIN ... Replacement logic ...

RETURN lv_element_collection; END update_collection;

Outgoing table view column as function return value.

Incoming table view column as

parameter.

Create target container for table view column.

Page 38: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Updating Table View Columns

CREATE OR REPLACE FUNCTION update_collection ...

BEGIN FOR i IN 1..old_element_collection.COUNT LOOP IF NOT old_element_collection(i) = old_element_value THEN lv_element_collection.EXTEND; lv_element_collection(lv_counter) := old_element_collection(i); ELSE lv_element_collection.EXTEND; lv_element_collection(lv_counter) := new_element_value; END IF; lv_counter := lv_counter + 1; END LOOP; ...

Replace matching values with the new value.

Replace non-matching values with the

pre-existing value.

Page 39: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Using Collections - Summary

• What are collections? • What are Oracle’s collection data types?

– Associative arrays – Variable size arrays – Nested tables

• How do you convert PL/SQL collections to SQL result sets?

• How do you update nested tables and table view columns inside tables?

Page 40: Using Collections · 2019-04-18 · What are Collections? • Collections are groups of elements that share the same data type. • Arrays – Have a fixed maximum number of elements

Using Collections

Questions & Answers