Oracle 23.26PL/SQLLab-verified · 2026-11-24

ORA-06533: Subscript beyond count

A nested table or VARRAY element was used with an index larger than the collection's current COUNT.

Oracle AI Database 26ai Free 23.26.3.0.0
ORA-06533: Subscript beyond count

Jump to a cause

  1. 01Assigning v(i) without EXTEND
  2. 02Loop upper bound is fixed (1..12) but fewer elements were loaded
  3. 03Reading v(1) after BULK COLLECT returned no rows

Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-24 · container image gvenzl/oracle-free:23-slim

ORA-06533 is raised when PL/SQL reads or writes v(n) of a nested table or VARRAY and n is larger than v.COUNT. These collections do not grow by themselves: an element exists only after the constructor, EXTEND or BULK COLLECT created it.

ORA-06512: at line N shows the line of the block where the bad index was used. Counting starts at DECLARE as line 1.

01Assigning v(i) without EXTEND

v_names was initialized with the empty constructor name_tab(), and the loop assigned v_names(i) := r.name. The first assignment, v_names(1), already failed at line 8, because the collection had no element 1.

The check shows COUNT is 0 after name_tab() and 1 after EXTEND. Calling v_names.EXTEND before each assignment and writing to v_names(v_names.LAST) loaded all 3 names (count = 3, last = PARK).

Setup
Setuprun as lab_a
CREATE TABLE emp (emp_id NUMBER, name VARCHAR2(20));
INSERT INTO emp VALUES (1, 'KIM');
INSERT INTO emp VALUES (2, 'LEE');
INSERT INTO emp VALUES (3, 'PARK');
COMMIT;
OUTPUT
Table created.


1 row created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
SET SERVEROUTPUT ON
DECLARE
  TYPE name_tab IS TABLE OF VARCHAR2(20);
  v_names name_tab := name_tab();
  i PLS_INTEGER := 0;
BEGIN
  FOR r IN (SELECT name FROM emp ORDER BY emp_id) LOOP
    i := i + 1;
    v_names(i) := r.name;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE('count = ' || v_names.COUNT);
END;
/
✕ ERROR
DECLARE
*
ERROR at line 1:
ORA-06533: Subscript beyond count
ORA-06512: at line 8
ORA-06512: at line 8
Help: https://docs.oracle.com/error-help/db/ora-06533/
Checkrun as lab_a
SET SERVEROUTPUT ON
DECLARE
  TYPE name_tab IS TABLE OF VARCHAR2(20);
  v_names name_tab := name_tab();
BEGIN
  DBMS_OUTPUT.PUT_LINE('count after name_tab() = ' || v_names.COUNT);
  v_names.EXTEND;
  DBMS_OUTPUT.PUT_LINE('count after EXTEND = ' || v_names.COUNT);
END;
/
OUTPUT
count after name_tab() = 0
count after EXTEND = 1

PL/SQL procedure successfully completed.

EXTEND adds one empty element at the end, and v.LAST is its index, so v(v.LAST) := ... always writes to the element that was just created.

After the fixrun as lab_a
SET SERVEROUTPUT ON
DECLARE
  TYPE name_tab IS TABLE OF VARCHAR2(20);
  v_names name_tab := name_tab();
BEGIN
  FOR r IN (SELECT name FROM emp ORDER BY emp_id) LOOP
    v_names.EXTEND;
    v_names(v_names.LAST) := r.name;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE('count = ' || v_names.COUNT);
  DBMS_OUTPUT.PUT_LINE('last = ' || v_names(v_names.LAST));
END;
/
✓ SUCCESS
count = 3
last = PARK

PL/SQL procedure successfully completed.

02Loop upper bound is fixed (1..12) but fewer elements were loaded

The code adds up 12 months with FOR m IN 1 .. 12, but MONTHLY_SALES has only 3 rows, so BULK COLLECT created 3 elements. v_amt(4) did not exist, and the error came at line 8.

Using the real size, FOR m IN 1 .. v_amt.COUNT, finished with months = 3, total = 370.

Setup
Setuprun as lab_a
CREATE TABLE monthly_sales (mon NUMBER, amount NUMBER);
INSERT INTO monthly_sales VALUES (1, 100);
INSERT INTO monthly_sales VALUES (2, 150);
INSERT INTO monthly_sales VALUES (3, 120);
COMMIT;
OUTPUT
Table created.


1 row created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
SET SERVEROUTPUT ON
DECLARE
  TYPE num_tab IS TABLE OF NUMBER;
  v_amt num_tab;
  v_total NUMBER := 0;
BEGIN
  SELECT amount BULK COLLECT INTO v_amt FROM monthly_sales ORDER BY mon;
  FOR m IN 1 .. 12 LOOP
    v_total := v_total + v_amt(m);
  END LOOP;
  DBMS_OUTPUT.PUT_LINE('total = ' || v_total);
END;
/
✕ ERROR
DECLARE
*
ERROR at line 1:
ORA-06533: Subscript beyond count
ORA-06512: at line 8
Help: https://docs.oracle.com/error-help/db/ora-06533/
Checkrun as lab_a
SELECT COUNT(*) FROM monthly_sales;
OUTPUT
  COUNT(*)
----------
         3

1 row selected.

Use 1 .. v.COUNT (or v.FIRST .. v.LAST) for the loop bound instead of a number you expect.

After the fixrun as lab_a
SET SERVEROUTPUT ON
DECLARE
  TYPE num_tab IS TABLE OF NUMBER;
  v_amt num_tab;
  v_total NUMBER := 0;
BEGIN
  SELECT amount BULK COLLECT INTO v_amt FROM monthly_sales ORDER BY mon;
  FOR m IN 1 .. v_amt.COUNT LOOP
    v_total := v_total + v_amt(m);
  END LOOP;
  DBMS_OUTPUT.PUT_LINE('months = ' || v_amt.COUNT || ', total = ' || v_total);
END;
/
✓ SUCCESS
months = 3, total = 370

PL/SQL procedure successfully completed.

03Reading v(1) after BULK COLLECT returned no rows

BULK COLLECT for customer 2 found no orders (COUNT(*) was 0). Unlike SELECT INTO, it does not raise NO_DATA_FOUND. It just leaves the collection empty. The next line read v_ids(1) and failed at line 6.

Checking v_ids.COUNT > 0 first printed no orders and finished normally.

Setup
Setuprun as lab_a
CREATE TABLE orders (order_id NUMBER, cust_id NUMBER, amount NUMBER);
INSERT INTO orders VALUES (1001, 1, 500);
INSERT INTO orders VALUES (1002, 1, 300);
COMMIT;
OUTPUT
Table created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
SET SERVEROUTPUT ON
DECLARE
  TYPE id_tab IS TABLE OF NUMBER;
  v_ids id_tab;
BEGIN
  SELECT order_id BULK COLLECT INTO v_ids FROM orders WHERE cust_id = 2 ORDER BY order_id;
  DBMS_OUTPUT.PUT_LINE('first order = ' || v_ids(1));
END;
/
✕ ERROR
DECLARE
*
ERROR at line 1:
ORA-06533: Subscript beyond count
ORA-06512: at line 6
Help: https://docs.oracle.com/error-help/db/ora-06533/
Checkrun as lab_a
SELECT COUNT(*) FROM orders WHERE cust_id = 2;
OUTPUT
  COUNT(*)
----------
         0

1 row selected.

Always check COUNT after BULK COLLECT before you read a fixed index such as (1).

After the fixrun as lab_a
SET SERVEROUTPUT ON
DECLARE
  TYPE id_tab IS TABLE OF NUMBER;
  v_ids id_tab;
BEGIN
  SELECT order_id BULK COLLECT INTO v_ids FROM orders WHERE cust_id = 2 ORDER BY order_id;
  IF v_ids.COUNT > 0 THEN
    DBMS_OUTPUT.PUT_LINE('first order = ' || v_ids(1));
  ELSE
    DBMS_OUTPUT.PUT_LINE('no orders');
  END IF;
END;
/
✓ SUCCESS
no orders

PL/SQL procedure successfully completed.

FAQ

What is the difference between ORA-06533 and ORA-06532?

ORA-06533 means the index is beyond the current COUNT of the collection. ORA-06532 means the index or EXTEND goes past the declared limit of a VARRAY (for example 4 in VARRAY(3)).

Why do I get ORA-06533 when I assign to a nested table?

A nested table has only the elements that were created. After name_tab() the COUNT is 0, so v(1) := ... fails. Call v.EXTEND first, as in the test.

Does BULK COLLECT raise NO_DATA_FOUND when there are no rows?

No. The collection is just empty (COUNT = 0). In the test, reading v_ids(1) afterwards gave ORA-06533. Check COUNT first.

How do I find which line raises ORA-06533?

Look at ORA-06512: at line N. N is counted from DECLARE (or BEGIN) of the block. In the tests it pointed exactly to the assignment or read with the bad index.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test; system is the DBA account. Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free 23.26.3.0.0