Oracle PL/SQL - DELETE Collection Method

Introduction

DELETE is a procedure that deletes elements from a collection.

This method has these forms:

  • DELETE deletes all elements from a collection of any type.
  • For an associative array or nested table (but not a varray):

DELETE(n) deletes the element whose index is n, if that element exists; otherwise, it does nothing.

DELETE(m,n) deletes all elements whose indexes are in the range m..n, if both m and n exist and m <= n; otherwise, it does nothing.

The following code shows how to use delete method.

Demo

SQL>
SQL> --DELETE Method with Nested Table
SQL> DECLARE
  2    nt nt_type := nt_type(11, 22, 33, 44, 55, 66);
  3  BEGIN--  w  ww. j ava 2s.  co m
  4    print_nt(nt);
  5    nt.DELETE(2);     -- Delete second element
  6    print_nt(nt);
  7
  8    nt(2) := 2222;    -- Restore second element
  9    print_nt(nt);
 10
 11    nt.DELETE(2, 4);  -- Delete range of elements
 12    print_nt(nt);
 13
 14    nt(3) := 3333;    -- Restore third element
 15    print_nt(nt);
 16
 17    nt.DELETE;        -- Delete all elements
 18    print_nt(nt);
 19  END;
 20  /
nt.(1) = 11
nt.(2) = 22
nt.(3) = 33
nt.(4) = 44
nt.(5) = 55
nt.(6) = 66
nt.(1) = 11
nt.(3) = 33
nt.(4) = 44
nt.(5) = 55
nt.(6) = 66
nt.(1) = 11
nt.(2) = 2222
nt.(3) = 33
nt.(4) = 44
nt.(5) = 55
nt.(6) = 66
nt.(1) = 11
nt.(5) = 55
nt.(6) = 66
nt.(1) = 11
nt.(3) = 3333
nt.(5) = 55
nt.(6) = 66
nt is empty

PL/SQL procedure successfully completed.

SQL>

Related Topics