Oracle PL/SQL - FIRST and LAST Methods for Associative Array

Introduction

For an associative array indexed by PLS_INTEGER, the first and last elements are those with the smallest and largest indexes, respectively.

The following code shows the values of FIRST and LAST for an associative array indexed by PLS_INTEGER.

Demo

SQL>
SQL>-- w  w w .  java2s  .  com
SQL> DECLARE
  2  TYPE IntMapType IS TABLE OF INTEGER INDEX BY PLS_INTEGER;
  3    IntMap  IntMapType;
  4
  5    PROCEDURE print_first_and_last IS
  6    BEGIN
  7      DBMS_OUTPUT.PUT_LINE('FIRST = ' || IntMap.FIRST);
  8      DBMS_OUTPUT.PUT_LINE('LAST = ' || IntMap.LAST);
  9    END print_first_and_last;
 10
 11  BEGIN
 12    IntMap(1) := 3;
 13    IntMap(2) := 6;
 14    IntMap(3) := 9;
 15    IntMap(4) := 12;
 16
 17    DBMS_OUTPUT.PUT_LINE('Before deletions:');
 18    print_first_and_last;
 19
 20    IntMap.DELETE(1);
 21    IntMap.DELETE(4);
 22
 23    DBMS_OUTPUT.PUT_LINE('After deletions:');
 24    print_first_and_last;
 25  END;
 26  /
Before deletions:
FIRST = 1
LAST = 4
After deletions:
FIRST = 2
LAST = 3

PL/SQL procedure successfully completed.

SQL>

Related Topics