How to Fix ORA-06532: Subscript Outside of Limit in Oracle

Keep varray indexes within the declared maximum size by checking COUNT against LIMIT, or use a nested table that can grow.

ORA-06532 is raised when PL/SQL code uses a collection index outside the range the collection type allows. With a varray, that means extending it past its declared maximum size, or using an index outside it. Index 0 or a negative index of a nested table or varray raises it too. The predefined exception is SUBSCRIPT_OUTSIDE_LIMIT. Check the varray's LIMIT before extending, or use a type without a fixed maximum.

Reproduce the Error

A varray of three seats is already full:

Example:

declare
  type seat_list is varray(3) of varchar2(4);
  v_seats seat_list := seat_list('1A', '1B', '1C');
begin
  v_seats.extend;
end;
/
declare
  type seat_list is varray(3) of varchar2(4);
  v_seats seat_list := seat_list('1A', '1B', '1C');
begin
  v_seats(0) := '1D';
end;
/

Output:

declare
*
ERROR at line 1:
ORA-06532: subscript outside of limit
ORA-06512: at line 5

declare
*
ERROR at line 1:
ORA-06532: subscript outside of limit
ORA-06512: at line 5

The first block extends past the limit of 3; the second uses index 0, which no varray or nested table has.

Fix: Check LIMIT Before EXTEND

Example:

set serveroutput on
declare
  type seat_list is varray(3) of varchar2(4);
  v_seats seat_list := seat_list('1A', '1B');
begin
  if v_seats.count < v_seats.limit then
    v_seats.extend;
    v_seats(v_seats.last) := '1C';
  end if;
  if v_seats.count < v_seats.limit then
    v_seats.extend;
  else
    dbms_output.put_line('Full: ' || v_seats.count || ' of ' || v_seats.limit);
  end if;
end;
/

Output:

Full: 3 of 3

PL/SQL procedure successfully completed.

The first EXTEND fits, as the varray holds two of three elements; the second is skipped because COUNT has reached LIMIT.

Things to Know

  • When the number of elements is not known in advance, use a nested table, which has no maximum size.
  • Indexes of varrays and nested tables start at 1.
  • An index within the limit but above COUNT raises ORA-06533 instead.

Related Guides

Conclusion

ORA-06532 means an index or EXTEND went beyond what the collection type allows. Compare COUNT with LIMIT before extending a varray, start indexes at 1, or switch to a nested table when the size can grow.

Get more Vinish.dev tutorials on Google

See my articles more often when you search Google.

Vinish Kapoor
Vinish Kapoor

An Oracle ACE, author of four books on Oracle APEX, SQL and PL/SQL, and Oracle Forms, and a software developer building Oracle database applications since 2001.

guest

0 Comments
Oldest
Newest Most Voted