Oracle PL/SQL - COUNT Method for Varray

Introduction

For a varray, COUNT always equals LAST.

If you increase or decrease the size of a varray (with the EXTEND or TRIM method), the value of COUNT changes.

The following code shows the values of COUNT and LAST for a varray after initialization with four elements, after EXTEND(3), and after TRIM(5).

Demo

SQL>
SQL> DECLARE-- from   w  w  w .java 2 s.  c o m
  2    TYPE NumList IS VARRAY(10) OF INTEGER;
  3    n NumList := NumList(1,3,5,7);
  4    PROCEDURE print_count_and_last IS
  5    BEGIN
  6      DBMS_OUTPUT.PUT('n.COUNT = ' || n.COUNT || ', ');
  7      DBMS_OUTPUT.PUT_LINE('n.LAST = ' || n.LAST);
  8    END  print_count_and_last;
  9
 10  BEGIN
 11    print_count_and_last;
 12
 13    n.EXTEND(3);
 14    print_count_and_last;
 15
 16    n.TRIM(5);
 17    print_count_and_last;
 18  END;
 19  /
n.COUNT = 4, n.LAST = 4
n.COUNT = 7, n.LAST = 7
n.COUNT = 2, n.LAST = 2

PL/SQL procedure successfully completed.

SQL>