Skip to main content

Oracle Collections

PL/SQL Collections

Index-by tables (Associative Arrays): They are arrays with the only difference that we can use numbers, string literals for subscript values. Associative Arrays are sets of key-value pairs, where key can be an integer or a string. There is no limitation on the size of Associative Arrays.

Syntax: TYPE type_name IS TABLE OF element_type [NOT NULL] INDEX BY [BINARY_INTEGER | PLS_INTEGER | VARCHAR2 (size_limit)] ;

Nested Tables: They are single dimensional, array-like tables, that can hold any number of elements inside another database table and can be manipulated by SQL as other database objects.

Syntax: TYPE type_name IS TABLE OF element_type [NOT NULL];

Varying Arrays: As the name suggests, varrays (Varying Arrays) are fixed length arrays that use sequential numbers as subscripts. varrays need to be dense and we cannot remove/delete individual elements from the array. Length is defined at design time and can be manipulated at runtime subject to maximum length defined. They are suitable in cases where all elements in the array are accessed in sequence.

Syntax: TYPE type_name IS [VARRAY | VARYING ARRAY] (size_limit) OF element_type [NOT NULL];

Using PL/SQL Collections in SQL statements

NESTED TABLE

CREATE TYPE Courseslist AS TABLE OF VARCHAR2(50);

CREATE TABLE student as OBJECT (
roll_no NUMBER(2),
name VARCHAR2(20),
courses Courseslist)
NESTED TABLE courses STORE AS courses_tab;

VARRAY

CREATE TYPE Courseslist AS VARRAY(50) OF VARCHAR2(20);

CREATE TABLE student (
roll_no NUMBER(2),
name VARCHAR2(20),
courses Courseslist);

USING COLLECTIONS IN SQL

DECLARE
TYPE Courseslist is TABLE OF VARCHAR2(20);
TYPE Courseslist2 is VARRAY(40) OF VARCHAR2(20);
my_courses Courseslist;
my_courses2 Courseslist2;
BEGIN
my_courses := Courseslist(’English’, ‘Mathematics’, ‘Science’);
my_courses2 := Courseslist2(’English’, ‘Mathematics’, ‘Science’);
INSERT INTO student values(12, ‘Ankur’, Courseslist(’English’, ‘Mathematics’, ‘Science’));
END;

Manipulating Individual Elements in Collections

Using subqueries within TABLE operator we can extract varrays and Nested tables from containing table and execute INSERT, UPDATE and DELETE operations.

Syntax:

BEGIN
INSERT INTO TABLE (SELECT courses FROM student where roll_no=2) VALUES (’German’);
DELETE TABLE (SELECT courses FROM student where roll_no = 1) WHERE course_strength < 5;
END;

USING COLLECTION METHODS

EXISTS - Check if an element exists in the collection.
Syntax : IF courses.EXISTS(i) THEN courses(i) = new_course; END IF;

COUNT - Count the elements in collection
Syntax : IF courses.COUNT > 20 THEN...

LIMIT - Check the Maximum Size of a collection
Syntax: IF new_courses.COUNT <>

FIRST and LAST - Retrieve the FIRST and LAST element from collection
Syntax: IF courses.FIRST = courses.LAST THEN ...

PRIOR and NEXT - Retrieve Previous and Next element of an index in collection.
Syntax: courses(i) = courses.NEXT(courses.PRIOR(i));

EXTEND - Append a null element at the end of collection.
EXTEND (n) - Append n null elements to the collection.
EXTEND (n,m) - Append n copies of element m to the collection.
Syntax: courses.EXTEND(2,1) : Appends 2 copies of 1st element of courses collection.

TRIM - Remove the last element from a collection.
TRIM(n) - Remove n elements from the end of a collection.
Syntax: courses.TRIM(5) : Remove last 5 elements from courses collection.

DELETE - Delete all elements from a collection
DELETE(n) - Delete nth element from an associative array with numeric key or a nested table. If key is string in Associative Array, element corresponding to the key value is deleted. If n is null, DELETE(n) does nothing.
DELETE(m, n) - DELETE elements in the range m-n from an associative array or nested table.

Varrays are dense, therefore, individual elements cannot be deleted from Varrays.

BULK BINDING

Assigning of values to PL/SQL variables in SQL statements is called binding. Use FORALL statement for bulk-bind input operations before sending them to SQL engine. It improves performance by minimizing context switching between SQL Engine and PL/SQL Engine and passing whole collections to SQL ENGINE at a time during execution.

Syntax:

DECLARE
TYPE NumList is VARRAY(20) OF NUMBER;
courses NumList := NumList(10, 20, 30);
BEGIN
FORALL i in courses.FIRST..courses.LAST
DELETE FROM student where course_id = courses(i);
END;

We can redefine the length of loop to execute the FORALL statement selectively. We may count number of rows affected, exceptions occurred in FORALL statement using SQL%BULK_ROWCOUNT, SQL%BULK_EXCEPTIONS attributes after the end of the loop.

BULK COLLECT

We use BULK COLLECT to bulk-bind output collections before returning to PL/SQL Engine as so to improve the performance, in a similar way, we used FORALL statement.

Syntax: SELECT courses BULK COLLECT INTO my_courses FROM student;

We can also use BULK COLLECT with cursors.

Syntax:

BEGIN
OPEN c1;
FETCH c1 BULK COLLECT INTO my_courses;
END;

LIMIT clause to limit the rows fetched for a BULK FETCH Operation

Syntax:

BEGIN
OPEN c1;
FETCH c1 BULK COLLECT INTO my_courses LIMIT 10;
END;

RETURNING INTO Clause

We can use RETURNING INTO clause to retrieve DML results into a collection.

Syntax:

BEGIN
DELETE FROM student WHERE marks < 50;
RETURNING courses BULK COLLECT INTO my_courses;
END;

We can use Host Arrays with Bulk Binds for output and input, as they are the most effective way to pass collections between database server and clients.

Syntax:

BEGIN
FORALL i in :lower .. :upper
DELETE FROM student WHERE roll_no := roll(i);
END;

Popular posts from this blog

iPod Nano 5G synchronization issues in Ubuntu

I was able to manage my iPod Nano using Ubuntu until now. When I recently upgraded to Ubuntu 11.04 Natty which was a clean install, a weird problem started. I could not synchronize my iPod using any application like Banshee, Rhythmbox, gtkpod, Amarok etc. On closer look, I realized that when I transferred any file, it was copied to the iPod but the database on iPod was not updated. None of the applications seemed to work and it was same error in each case. Since the iPod database was not updated, iPod could not see the file and was unable to play it. It was an iPod Nano generation 5, which comes with a video camera on the back side. To debug the issue, I executed Banshee in debug mode (banshee -debug on the terminal), and I noticed the following error when trying to transfer any file on iPod. ‘Failed to save iPod database – GLib.GException: Failed to generate sqlite database (in `libgpod-sharp)’ I searched for solutions on Internet but was confused at various solutions suggest...

Sydney Trip

I went to Sydney on a business trip for 2-3 weeks. Since, it was a business trip I was going to travel alone and miss my family. Therefore, I appreciated the short duration of the trip but it did not feel like a short one. Most of it was work only and some time for me during the two weekends. I had decided to make the most of available time instead of idling and feel more homesick. Also, I was going to stay in the CBD area so it was a big advantageous factor since not much effort was to be spent for sightseeing. The journey began with the longest flight I’ve ever been on (17 hours) with a stopover at Bangkok for 2 hours. I had a terrible time in the flight from Delhi to Bangkok; I was struggling a lot in the airplane seats and couldn’t sleep at all. However, the flight from Bangkok to Sydney was much better. On my flight from Bangkok to Sydney, an Australian lady was seated at the window seat on my right hand side. She asked me if I wanted to swap my seat with hers as I had starte...

Busy or Lazy?

Either of the two. But that’s been the case after my wedding. I’ve not done a blog post, neither I did anything on Facebook, Twitter, Google+ for that matter. Whatever! Better late than never. So, in order of priority, I wanted to write down a few important events that happened since my last post - We were blessed with a baby daughter – Naisha. Literal meaning of Naisha is ‘Special’ and she justifies this meaning completely. She’s beautiful, cranky and a fighter. She’s awesome! Naisha is one creature in the house. She just celebrated her first birthday dressed up as a Barbie, which was cute. All the day she’s running around the house and interpreting various things and objects in her unique style. I cannot understand much of it, but most of it appears very logical somehow. I switched from X to Y organization. It was a long pending decision for me and thankfully it worked out this time. It took its own time and I was recruited after long six months of interviewing process. A...