ORA-06512 is not an error of its own. It is a line of the error stack that says where an error passed through PL/SQL code: which program unit, and which line. The real error is the first line of the stack, above the ORA-06512 lines. To fix the problem, read that first error, then follow the ORA-06512 lines to the code that raised it.
See It in an Error Stack
One procedure checks a quantity and raises an error; another calls it:
Example:
create or replace procedure demo_check_qty (p_qty number) is
begin
if p_qty > 10 then
raise_application_error(-20001, 'At most 10 seats per order');
end if;
end;
/
create or replace procedure demo_place_order (p_qty number) is
begin
demo_check_qty(p_qty);
end;
/
exec demo_place_order(12)Output:
Procedure DEMO_CHECK_QTY compiled Procedure DEMO_PLACE_ORDER compiled BEGIN demo_place_order(12); END; * ERROR at line 1: ORA-20001: At most 10 seats per order ORA-06512: at "NIMBUS.DEMO_CHECK_QTY", line 4 ORA-06512: at "NIMBUS.DEMO_PLACE_ORDER", line 3 ORA-06512: at line 1
- ORA-20001 is the actual error, the one to fix.
- The ORA-06512 lines go from where it was raised, line 4 of DEMO_CHECK_QTY, outward to the caller, line 3 of DEMO_PLACE_ORDER, and to line 1 of the anonymous block.
- For an anonymous block, the line number counts from its first line, DECLARE or BEGIN.
Find the Line in the Source
Example:
-- the line each ORA-06512 points to select name, line, text from user_source where (name = 'DEMO_CHECK_QTY' and line = 4) or (name = 'DEMO_PLACE_ORDER' and line = 3) order by name desc;
Output:
NAME LINE TEXT ___________________ _______ _____________________________________________________________________ DEMO_PLACE_ORDER 3 demo_check_qty(p_qty); DEMO_CHECK_QTY 4 raise_application_error(-20001, 'At most 10 seats per order');
Keep the Line Numbers When You Handle Errors
An exception handler that logs SQLERRM alone loses the location. DBMS_UTILITY.FORMAT_ERROR_BACKTRACE returns the ORA-06512 lines:
Example:
set serveroutput on
begin
demo_place_order(12);
exception
when others then
dbms_output.put_line(sqlerrm);
dbms_output.put_line(dbms_utility.format_error_backtrace);
end;
/Output:
ORA-20001: At most 10 seats per order ORA-06512: at "NIMBUS.DEMO_CHECK_QTY", line 4 ORA-06512: at "NIMBUS.DEMO_PLACE_ORDER", line 3 ORA-06512: at line 2 PL/SQL procedure successfully completed.
Things to Know
- A handler that catches and raises a new error, for example with RAISE_APPLICATION_ERROR, starts a new stack at the handler; pass TRUE as its third argument to keep the original errors.
- An ORA-06512 line without a program name, such as "at line 1", refers to the anonymous block that was run.
- ORA-06512 lines also appear below errors raised by Oracle, such as ORA-01403 or ORA-01476.
Related Guides
- Error Handling with Dbms_utility.format_error_backtrace
- How to Read the Call Stack with UTL_CALL_STACK
Conclusion
ORA-06512 shows where an error traveled through PL/SQL. Fix the error at the top of the stack, use the first ORA-06512 line to find where it was raised, and log FORMAT_ERROR_BACKTRACE in handlers so the location is not lost.
