Oracle PL/SQL - EXISTS Collection Method

Introduction

EXISTS tells you whether the specified element of a varray or nested table exists.

EXISTS(n) returns TRUE if the nth element of the collection exists and FALSE otherwise.

If n is out of range, EXISTS returns FALSE.

For a deleted element, EXISTS(n) returns FALSE, even if DELETE kept a placeholder for it.

The following code test the EXISTS Method with Nested Table

Demo

SQL>
SQL> DECLARE--  w w  w  . java 2s.  co m
  2    TYPE NumList IS TABLE OF INTEGER;
  3    n NumList := NumList(1,3,5,7);
  4  BEGIN
  5    n.DELETE(2); -- Delete second element
  6
  7    FOR i IN 1..6 LOOP
  8      IF n.EXISTS(i) THEN
  9        DBMS_OUTPUT.PUT_LINE('n(' || i || ') = ' || n(i));
 10      ELSE
 11        DBMS_OUTPUT.PUT_LINE('n(' || i || ') does not exist');
 12      END IF;
 13    END LOOP;
 14  END;
 15  /
n(1) = 1
n(2) does not exist
n(3) = 5
n(4) = 7
n(5) does not exist
n(6) does not exist

PL/SQL procedure successfully completed.

SQL>