Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.4K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 546 SQLcl
- 4K SQL Developer Data Modeler
- 187.1K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 443 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
ORA-06531: Reference to uninitialized collection

set SERVEROUTPUT ON
Declare
type t is table of Varchar2(6);
v_t1 t:= t();--initialized collection variable
Begin
v_t1.extend(6);
v_t1(1):='Lokesh';
v_t1(2):='Ashok';
v_t1(3):='Mahesh';
v_t1(4):='Ramu';
v_t1(5):='Ajay';
v_t1(6):='Irani chawla';
for i in v_t1.first..v_t1.last loop
dbms_output.put_line('Hi v_t1='||v_t1(i));
end loop;
forall i in v_t1.first..v_t1.last save exceptions
insert into temp99 values(v_t1(i));
Exception when others then
dbms_output.put_line('bulk exception count='||sql%bulk_exceptions.count);
for i in 1..sql%bulk_exceptions.count loop
dbms_output.put_line('bulk exception index='||sql%bulk_exceptions(i).error_index);
dbms_output.put_line('bulk exception code='||sql%bulk_exceptions(i).error_code);
end loop;
End;
My table structure is
create table temp99(tname Varchar2(6))
Getting error like
Error report -
ORA-06531: Reference to uninitialized collection
ORA-06512: at line 22
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 12
06531. 00000 - "Reference to uninitialized collection"
*Cause: An element or member function of a nested table or varray
was referenced (where an initialized collection is needed)
without the collection having been initialized.
*Action: Initialize the collection with an appropriate constructor
or whole-object assignment.
I want to use error index and error code, so my table structure is varchar2(6) only.
If i am removing v_t1(6):='Irani chawla'; collection vallue then executing fine but when i am using
v_t1(6):='Irani chawla'; collection value facing same issue. please suggest i went through google also but not getting any idea
Best Answer
-
HI, @Lokesh A
The error message:
ORA-06531: Reference to uninitialized collection
ORA-06512: at line 22
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 12
says the real error occurred at line 12. SQL%BULK_EXCEPTIONS is initialized at line 22, when you say SAVE EXCEPTIONS . If you change line 2 to
type t is table of Varchar2(20);
then no error will occur before line 22, so when an error does occur in the INSERT statement on line 23, SQL%BULK_EXCEPTIONS can catch it.
Answers
-
v_t1(6):='Irani chawla'; will raise an exception. But that is not a bulk exception.
So, all that is because of poor exception handling.
Try the below and see:
Declare type t is table of Varchar2(6); v_t1 t:= t();--initialized collection variable Begin v_t1.extend(6); v_t1(1):='Lokesh'; v_t1(2):='Ashok'; v_t1(3):='Mahesh'; v_t1(4):='Ramu'; v_t1(5):='Ajay'; v_t1(6):='Irani chawla'; for i in v_t1.first..v_t1.last loop dbms_output.put_line('Hi v_t1='||v_t1(i)); end loop; begin forall i in v_t1.first..v_t1.last save exceptions insert into temp99 values(v_t1(i)); exception when others then dbms_output.put_line('bulk exception count='||sql%bulk_exceptions.count); for i in 1..sql%bulk_exceptions.count loop dbms_output.put_line('bulk exception index='||sql%bulk_exceptions(i).error_index); dbms_output.put_line('bulk exception code='||sql%bulk_exceptions(i).error_code); end loop; end; /* Exception when others then dbms_output.put_line('bulk exception count='||sql%bulk_exceptions.count); for i in 1..sql%bulk_exceptions.count loop dbms_output.put_line('bulk exception index='||sql%bulk_exceptions(i).error_index); dbms_output.put_line('bulk exception code='||sql%bulk_exceptions(i).error_code); end loop; */ End;
By increasing the size of that element you will run the script successfully.
Thus, try:
create table temp99(tname Varchar2(100)); Declare type t is table of temp99.tname%type; v_t1 t:= t();--initialized collection variable Begin v_t1.extend(6); v_t1(1):='Lokesh'; v_t1(2):='Ashok'; v_t1(3):='Mahesh'; v_t1(4):='Ramu'; v_t1(5):='Ajay'; v_t1(6):='Irani chawla'; for i in v_t1.first..v_t1.last loop dbms_output.put_line('Hi v_t1='||v_t1(i)); end loop; begin forall i in v_t1.first..v_t1.last save exceptions insert into temp99 values(v_t1(i)); exception when others then dbms_output.put_line('bulk exception count='||sql%bulk_exceptions.count); for i in 1..sql%bulk_exceptions.count loop dbms_output.put_line('bulk exception index='||sql%bulk_exceptions(i).error_index); dbms_output.put_line('bulk exception code='||sql%bulk_exceptions(i).error_code); end loop; end; /* Exception when others then dbms_output.put_line('bulk exception count='||sql%bulk_exceptions.count); for i in 1..sql%bulk_exceptions.count loop dbms_output.put_line('bulk exception index='||sql%bulk_exceptions(i).error_index); dbms_output.put_line('bulk exception code='||sql%bulk_exceptions(i).error_code); end loop; */ End;
-
Normally, you should refer sql%bulk_exceptions only after forall was run. sql%bulk_exceptions is not initialized unless that forall is executed. So, the exception that occurred before the forall passed the control to the exception handler at the very end of the code before having executed the forall and thus found sql%bulk_exceptions collection not initialized.
-
SAVE EXCEPTION alone doesn't initialize collection sql%bulk_exceptions. It is initialized only if there were FORALL exceptions. Add the following to declaration section:
DML_ARRAY_ERRORS EXCEPTION; PRAGMA EXCEPTION_INIT (DML_ARRAY_ERRORS, -24381);
And change EXCEPTIONS clause to:
exception when DML_ARRAY_ERRORS then dbms_output.put_line('bulk exception count='||sql%bulk_exceptions.count); for i in 1..sql%bulk_exceptions.count loop dbms_output.put_line('bulk exception index='||sql%bulk_exceptions(i).error_index); dbms_output.put_line('bulk exception code='||sql%bulk_exceptions(i).error_code); end loop;
SY.
-
HI, @Lokesh A
The error message:
ORA-06531: Reference to uninitialized collection
ORA-06512: at line 22
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 12
says the real error occurred at line 12. SQL%BULK_EXCEPTIONS is initialized at line 22, when you say SAVE EXCEPTIONS . If you change line 2 to
type t is table of Varchar2(20);
then no error will occur before line 22, so when an error does occur in the INSERT statement on line 23, SQL%BULK_EXCEPTIONS can catch it.