How to Fix ORA-06533: Subscript Beyond Count in Oracle

Extend nested tables and varrays before assigning new elements, check COUNT or EXISTS before reading, and see how associative arrays differ.

ORA-06533 is raised when PL/SQL code reads or writes a nested table or varray element whose index is greater than the collection's current number of elements. These collections grow only through EXTEND, so element 3 of a two-element collection does not exist yet, even for assignment. The predefined exception is SUBSCRIPT_BEYOND_COUNT. Extend the collection before assigning, and check COUNT or EXISTS before reading.

Reproduce the Error

Example:

declare
  type num_list is table of number;
  v_list num_list := num_list(10, 20);
begin
  v_list(3) := 30;
end;
/
declare
  type num_list is table of number;
  v_list num_list := num_list(10, 20);
begin
  dbms_output.put_line(v_list(3));
end;
/

Output:

declare
*
ERROR at line 1:
ORA-06533: Subscript beyond count
ORA-06512: at line 5

declare
*
ERROR at line 1:
ORA-06533: Subscript beyond count
ORA-06512: at line 5

Writing and reading element 3 both fail, because the collection has two elements.

Fix: EXTEND First, and Check Before Reading

Example:

set serveroutput on
declare
  type num_list is table of number;
  v_list num_list := num_list(10, 20);
begin
  v_list.extend;                 -- make room for element 3
  v_list(3) := 30;
  for i in 1 .. v_list.count loop
    dbms_output.put_line(i || ': ' || v_list(i));
  end loop;
  if v_list.exists(4) then
    dbms_output.put_line(v_list(4));
  else
    dbms_output.put_line('No element 4');
  end if;
end;
/

Output:

1: 10
2: 20
3: 30
No element 4

PL/SQL procedure successfully completed.

EXTEND adds element 3, the loop stays within 1 to COUNT, and EXISTS tests an index without raising an error.

Associative Arrays Behave Differently

An associative array has no count limit: any index can be assigned, and reading a missing one raises NO_DATA_FOUND:

Example:

-- an associative array has no count limit: a missing key raises NO_DATA_FOUND instead
declare
  type num_map is table of number index by pls_integer;
  v_map num_map;
begin
  v_map(1) := 10;
  v_map(100) := 30;              -- allowed, no EXTEND needed
  dbms_output.put_line(v_map(2));
end;
/

Output:

declare
*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at line 7

Things to Know

  • EXTEND(n) adds n elements at once, which is faster than one EXTEND per element in a loop.
  • After DELETE of single elements, a nested table has gaps; loop with FIRST and NEXT instead of 1 to COUNT.

Related Guides

Conclusion

ORA-06533 means an index above the collection's COUNT. Call EXTEND before assigning new elements, loop within COUNT, and use EXISTS to test an index safely.

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