Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-25 · container image gvenzl/oracle-free:23-slim
PL/SQL collections start at index 1. Using index 0 (or a negative number) raises ORA-06532, and so does extending a VARRAY(n) beyond n elements. The ORA-06512: at line N line under it tells you which line of the block hit the bad index.
Note the difference from ORA-06533: reading one past the end of a collection (for example v(4) when it has 3 elements) gives ORA-06533 (subscript beyond count), not ORA-06532.
01EXTEND past the size limit of a VARRAY
The type was phone_list AS VARRAY(3) OF VARCHAR2(20), and customer 1 had 4 phone numbers. The loop did v.EXTEND for each row, and the 4th EXTEND failed with ORA-06532 at line 5.
ALTER TYPE phone_list MODIFY LIMIT 10 CASCADE raised the limit; user_coll_types then showed UPPER_BOUND 10, and the same block printed count=4.
Setup
CREATE TYPE phone_list AS VARRAY(3) OF VARCHAR2(20);
/
CREATE TABLE contacts (cust_id NUMBER, phone VARCHAR2(20));
INSERT INTO contacts VALUES (1, '010-1111-1111');
INSERT INTO contacts VALUES (1, '02-222-2222');
INSERT INTO contacts VALUES (1, '031-333-3333');
INSERT INTO contacts VALUES (1, '010-4444-4444');
COMMIT;Type created.
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
Commit complete.SET SERVEROUTPUT ON
DECLARE
v phone_list := phone_list();
BEGIN
FOR r IN (SELECT phone FROM contacts WHERE cust_id = 1) LOOP
v.EXTEND;
v(v.LAST) := r.phone;
END LOOP;
DBMS_OUTPUT.PUT_LINE('count=' || v.COUNT);
END;
/DECLARE
*
ERROR at line 1:
ORA-06532: subscript outside of limit
ORA-06512: at line 5
ORA-06512: at line 5
Help: https://docs.oracle.com/error-help/db/ora-06532/ALTER TYPE phone_list MODIFY LIMIT 10 CASCADE;
COLUMN type_name FORMAT A12
SELECT type_name, upper_bound FROM user_coll_types WHERE type_name = 'PHONE_LIST';Type altered.
TYPE_NAME UPPER_BOUND
------------ -----------
PHONE_LIST 10
1 row selected.CASCADE also applies the change to objects that depend on the type. Pick a limit with room for real data, not just today's maximum.
SET SERVEROUTPUT ON
DECLARE
v phone_list := phone_list();
BEGIN
FOR r IN (SELECT phone FROM contacts WHERE cust_id = 1) LOOP
v.EXTEND;
v(v.LAST) := r.phone;
END LOOP;
DBMS_OUTPUT.PUT_LINE('count=' || v.COUNT);
END;
/count=4
PL/SQL procedure successfully completed.02A loop starts at index 0
Written out of habit from 0-based languages, FOR i IN 0 .. v.COUNT - 1 read v(0) first and failed with ORA-06532 at line 7, before printing anything.
After BULK COLLECT, v.FIRST was 1 and v.LAST was 3. Changing the loop to FOR i IN 1 .. v.COUNT printed PEN, NOTE, BAG.
Setup
CREATE TABLE items (item_id NUMBER, item_name VARCHAR2(20));
INSERT INTO items VALUES (1, 'PEN');
INSERT INTO items VALUES (2, 'NOTE');
INSERT INTO items VALUES (3, 'BAG');
COMMIT;Table created.
1 row created.
1 row created.
1 row created.
Commit complete.SET SERVEROUTPUT ON
DECLARE
TYPE name_tab IS TABLE OF VARCHAR2(20);
v name_tab;
BEGIN
SELECT item_name BULK COLLECT INTO v FROM items ORDER BY item_id;
FOR i IN 0 .. v.COUNT - 1 LOOP
DBMS_OUTPUT.PUT_LINE(i || ': ' || v(i));
END LOOP;
END;
/DECLARE
*
ERROR at line 1:
ORA-06532: subscript outside of limit
ORA-06512: at line 7
Help: https://docs.oracle.com/error-help/db/ora-06532/SET SERVEROUTPUT ON
DECLARE
TYPE name_tab IS TABLE OF VARCHAR2(20);
v name_tab;
BEGIN
SELECT item_name BULK COLLECT INTO v FROM items ORDER BY item_id;
DBMS_OUTPUT.PUT_LINE('FIRST=' || v.FIRST || ' LAST=' || v.LAST);
END;
/FIRST=1 LAST=3
PL/SQL procedure successfully completed.SET SERVEROUTPUT ON
DECLARE
TYPE name_tab IS TABLE OF VARCHAR2(20);
v name_tab;
BEGIN
SELECT item_name BULK COLLECT INTO v FROM items ORDER BY item_id;
FOR i IN 1 .. v.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(i || ': ' || v(i));
END LOOP;
END;
/1: PEN
2: NOTE
3: BAG
PL/SQL procedure successfully completed.03Reading v(i-1) on the first element
A day-over-day difference was calculated as v(i) - v(i - 1) in a loop starting at 1. On the first pass that reads v(0), so the block failed with ORA-06532.
v.PRIOR(1) returned NULL: the first element has no previous one. Starting the loop at 2 (FOR i IN 2 .. v.COUNT) printed day 2 diff=50 and day 3 diff=-30.
Setup
CREATE TABLE daily_sales (sale_date DATE, amount NUMBER);
INSERT INTO daily_sales VALUES (DATE '2024-03-01', 100);
INSERT INTO daily_sales VALUES (DATE '2024-03-02', 150);
INSERT INTO daily_sales VALUES (DATE '2024-03-03', 120);
COMMIT;Table created.
1 row created.
1 row created.
1 row created.
Commit complete.SET SERVEROUTPUT ON
DECLARE
TYPE num_tab IS TABLE OF NUMBER;
v num_tab;
BEGIN
SELECT amount BULK COLLECT INTO v FROM daily_sales ORDER BY sale_date;
FOR i IN 1 .. v.COUNT LOOP
DBMS_OUTPUT.PUT_LINE('day ' || i || ' diff=' || (v(i) - v(i - 1)));
END LOOP;
END;
/DECLARE
*
ERROR at line 1:
ORA-06532: subscript outside of limit
ORA-06512: at line 7
Help: https://docs.oracle.com/error-help/db/ora-06532/SET SERVEROUTPUT ON
DECLARE
TYPE num_tab IS TABLE OF NUMBER;
v num_tab;
BEGIN
SELECT amount BULK COLLECT INTO v FROM daily_sales ORDER BY sale_date;
DBMS_OUTPUT.PUT_LINE('PRIOR(1)=' || NVL(TO_CHAR(v.PRIOR(1)), 'NULL'));
END;
/PRIOR(1)=NULL
PL/SQL procedure successfully completed.If you also need a line for the first day, handle i = 1 separately instead of reading v(0).
SET SERVEROUTPUT ON
DECLARE
TYPE num_tab IS TABLE OF NUMBER;
v num_tab;
BEGIN
SELECT amount BULK COLLECT INTO v FROM daily_sales ORDER BY sale_date;
FOR i IN 2 .. v.COUNT LOOP
DBMS_OUTPUT.PUT_LINE('day ' || i || ' diff=' || (v(i) - v(i - 1)));
END LOOP;
END;
/day 2 diff=50
day 3 diff=-30
PL/SQL procedure successfully completed.FAQ
What does ORA-06532 subscript outside of limit mean?
A collection was accessed with an index of 0 or less, or a VARRAY was extended past its declared maximum size.
What is the difference between ORA-06532 and ORA-06533?
ORA-06532 is an index outside the allowed range (0, negative, or beyond a VARRAY's limit). ORA-06533 is an index past the current number of elements, such as v(4) when there are 3.
How do I increase the size of a VARRAY type?
Run ALTER TYPE type_name MODIFY LIMIT n CASCADE and check upper_bound in user_coll_types.
Do PL/SQL collections start at 0 or 1?
At 1. After BULK COLLECT the first index is 1 and the last is COUNT, so loop with FOR i IN 1 .. v.COUNT.
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