πŸ“š Complete Guide to PL/SQL Collections (With Examples)

πŸ“š Complete Guide to PL/SQL Collections (With Examples)

Working with multiple rows of data efficiently is a common requirement in PL/SQL. This is where Collections come into play β€” one of the most powerful features in Oracle.

In this blog, we’ll explore what collections are, their types, and real-time examples to help you master them.


πŸ”Ή What is a Collection?

A Collection is a homogeneous, single-dimensional data structure that stores multiple elements of the same datatype.

πŸ‘‰ In simple terms:

A Collection is an array in Oracle Database.


πŸ”₯ Types of Collections

Oracle supports three types of collections:

  1. Nested Tables
  2. VARRAYs (Variable Size Arrays)
  3. Associative Arrays

1️⃣ Nested Tables

πŸ“Œ Definition

  • Persistent collection (can be stored in database)
  • Unbounded (no size limit)
  • Stored as a separate table internally

πŸ‘‰ Think of it as a table inside a table


πŸ”Ή Syntax

TYPE nested_table_name IS TABLE OF element_type [NOT NULL];

πŸ”Ή Example 1: Associative Style Looping

DECLARE
 TYPE employee IS TABLE OF NUMBER
 INDEX BY VARCHAR2(64);

 srl_employee employee;
 i VARCHAR2(64);

BEGIN
 srl_employee('DDN') := 4;
 srl_employee('HUBLY') := 6;
 srl_employee('BANGALORE') := 10;

 i := srl_employee.FIRST;

 WHILE i IS NOT NULL LOOP
   DBMS_OUTPUT.PUT_LINE('Employees in ' || i || ' is ' || srl_employee(i));
   i := srl_employee.NEXT(i);
 END LOOP;
END;

πŸ”Ή Example 2: Simple Nested Table

DECLARE
 TYPE list_of_numbers IS TABLE OF NUMBER;
 var_nt list_of_numbers := list_of_numbers(9,18,27,36);

BEGIN
 DBMS_OUTPUT.PUT_LINE('Index 1: ' || var_nt(1));
 DBMS_OUTPUT.PUT_LINE('Index 2: ' || var_nt(2));
END;

πŸ”Ή Example 3: Nested Table in Database

CREATE TYPE emp_hobies IS TABLE OF VARCHAR2(100);

CREATE TABLE employees (
    emp_id NUMBER,
    emp_name VARCHAR2(20),
    emp_hobie emp_hobies
) NESTED TABLE emp_hobie STORE AS nested_tab_space;

Insert Data

INSERT INTO employees VALUES (1, 'Roopa', emp_hobies('Listening Songs','Traveling'));

Query Nested Table

SELECT * FROM TABLE (
  SELECT emp_hobie FROM employees WHERE emp_id = 1
);

2️⃣ VARRAY (Variable Size Array)

πŸ“Œ Definition

  • Persistent collection
  • Stored inline with table
  • Fixed size (bounded)

πŸ”Ή Syntax

CREATE TYPE type_name AS VARRAY(size_limit) OF element_type;

πŸ”Ή Example 1: PL/SQL Block

DECLARE 
 TYPE emp_number IS VARRAY(6) OF NUMBER;
 vry_obj emp_number := emp_number();

BEGIN
 FOR i IN 1..6 LOOP
   vry_obj.EXTEND;
   vry_obj(i) := 10*i;
   DBMS_OUTPUT.PUT_LINE(vry_obj(i));
 END LOOP;
END;

πŸ”Ή Example 2: Stored in Table

CREATE TYPE emp_num IS VARRAY(15) OF NUMBER;

CREATE TABLE emp (
    emp_number emp_num,
    emp_name VARCHAR2(200)
);

INSERT INTO emp VALUES(emp_num(1,2,3), 'Ankur');

Query VARRAY

SELECT emp.emp_name, vry.COLUMN_VALUE
FROM emp, TABLE(emp.emp_number) vry;

❓ Important Questions

πŸ‘‰ Can we change size of VARRAY?

❌ No, size is fixed after creation

πŸ‘‰ Can we store VARRAY in DB?

βœ… Yes

πŸ‘‰ Is VARRAY bounded?

βœ… Yes


3️⃣ Associative Array

πŸ“Œ Definition

  • Non-persistent (memory only)
  • Cannot be stored in database
  • Exists only during session

πŸ”Ή Syntax

TYPE array_name IS TABLE OF datatype
INDEX BY datatype;

πŸ”Ή Example

DECLARE
TYPE emp IS TABLE OF NUMBER INDEX BY VARCHAR2(20);
srl_emp emp;
emp_loop VARCHAR2(20);
BEGIN
srl_emp('Roopa') := 1;
srl_emp('Ankur') := 2;
emp_loop := srl_emp.FIRST;
WHILE emp_loop IS NOT NULL LOOP
DBMS_OUTPUT.PUT_LINE(emp_loop || ' -> ' || srl_emp(emp_loop));
emp_loop := srl_emp.NEXT(emp_loop);
END LOOP;
END;

πŸš€ BULK COLLECT

πŸ“Œ What is it?

Used to fetch multiple rows at once into a collection β†’ improves performance.


πŸ”Ή Syntax

FETCH cursor_name BULK COLLECT INTO collection;

πŸ”Ή Example

DECLARE
 CURSOR exp_cur IS SELECT emp_name FROM employees;

 TYPE emp_list IS TABLE OF VARCHAR2(20);
 fname emp_list;

BEGIN
 OPEN exp_cur;
 LOOP
   FETCH exp_cur BULK COLLECT INTO fname;
   EXIT WHEN fname.COUNT = 0;

   FOR i IN fname.FIRST..fname.LAST LOOP
     DBMS_OUTPUT.PUT_LINE(fname(i));
   END LOOP;
 END LOOP;
 CLOSE exp_cur;
END;

⚑ FORALL

πŸ“Œ What is it?

Used for bulk DML operations (INSERT, UPDATE, DELETE) β†’ very fast.


πŸ”Ή Syntax

FORALL i IN collection.FIRST..collection.LAST
   INSERT INTO table VALUES collection(i);

πŸ“Š Collection Methods

Collections come with built-in methods:

  • .COUNT β†’ number of elements
  • .FIRST β†’ first index
  • .LAST β†’ last index
  • .NEXT(index) β†’ next element
  • .PRIOR(index) β†’ previous element
  • .EXTEND β†’ add elements
  • .DELETE β†’ remove elements

🧠 Assignment

Try these:

βœ… Find distinct hobbies

βœ… Sort hobbies by popularity (descending)


🎯 Final Thoughts

PL/SQL Collections are extremely powerful for:

βœ” Handling bulk data

βœ” Improving performance

βœ” Writing cleaner code

βœ” Reducing context switching

If you’re working with large datasets or loops in PL/SQL, mastering collections is a must πŸš€

Love coding? Me too! Let’s keep in touch – subscribe to my website for regular chats on Oracle APEX, PL/SQL,SQL JavaScript, and CSS.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *