Oracle PL/SQL - COUNT Method for Nested Table

Introduction

For a nested table, COUNT equals LAST unless you delete elements from the middle of the nested table, in which case COUNT is smaller than LAST.

The following code shows the values of COUNT and LAST for a nested table.

Demo

SQL>
SQL> DECLARE-- from   ww w .  j a va 2s.c om
  2    TYPE NumList IS TABLE OF INTEGER;
  3    n NumList := NumList(1,3,5,7);
  4
  5    PROCEDURE print_count_and_last IS
  6    BEGIN
  7      DBMS_OUTPUT.PUT('n.COUNT = ' || n.COUNT || ', ');
  8      DBMS_OUTPUT.PUT_LINE('n.LAST = ' || n.LAST);
  9    END  print_count_and_last;
 10
 11  BEGIN
 12    print_count_and_last;
 13
 14    n.DELETE(3);  -- Delete third element
 15    print_count_and_last;
 16
 17    n.EXTEND(2);  -- Add two null elements to end
 18    print_count_and_last;
 19
 20    FOR i IN 1..8 LOOP
 21      IF n.EXISTS(i) THEN
 22        IF n(i) IS NOT NULL THEN
 23          DBMS_OUTPUT.PUT_LINE('n(' || i || ') = ' || n(i));
 24        ELSE
 25          DBMS_OUTPUT.PUT_LINE('n(' || i || ') = NULL');
 26        END IF;
 27      ELSE
 28        DBMS_OUTPUT.PUT_LINE('n(' || i || ') does not exist');
 29      END IF;
 30    END LOOP;
 31  END;
 32  /
n.COUNT = 4, n.LAST = 4
n.COUNT = 3, n.LAST = 4
n.COUNT = 5, n.LAST = 6
n(1) = 1
n(2) = 3
n(3) does not exist
n(4) = 7
n(5) = NULL
n(6) = NULL
n(7) does not exist
n(8) does not exist

PL/SQL procedure successfully completed.

SQL>