Oracle PL/SQL - Nested Table of Standalone Type

Introduction

The following code defines a standalone nested table type, nt_type.

Then it creates a standalone procedure to print a variable of that type, print_nt.

Demo

SQL>
SQL>--   ww  w .  j  ava 2  s  . co m
SQL> CREATE OR REPLACE TYPE nt_type IS TABLE OF NUMBER;
  2  /

Type created.

SQL> CREATE OR REPLACE PROCEDURE print_nt (nt nt_type) IS
  2    i  NUMBER;
  3  BEGIN
  4    i := nt.FIRST;
  5
  6    IF i IS NULL THEN
  7      DBMS_OUTPUT.PUT_LINE('nt is empty');
  8    ELSE
  9      WHILE i IS NOT NULL LOOP
 10        DBMS_OUTPUT.PUT('nt.(' || i || ') = '); print(nt(i));
 11        i := nt.NEXT(i);
 12      END LOOP;
 13    END IF;
 14  END print_nt;
 15  /

Procedure created.

SQL> DECLARE
  2    nt nt_type := nt_type();  -- nested table variable initialized to empty
  3  BEGIN
  4    print_nt(nt);
  5    nt := nt_type(2, 3, 5, 8);
  6    print_nt(nt);
  7  END;
  8  /
nt is empty
nt.(1) = 2
nt.(2) = 3
nt.(3) = 5
nt.(4) = 8

PL/SQL procedure successfully completed.

SQL>

Related Topic