How to resolve ORA-06512 Error Message in Oracle database

Learn the reason and how to unravel the ORA-06512 error message in Oracle.

Description

When you stumble upon an ORA-06512 error, the following error message will appear:

ORA-06512: at line <number>

Cause

This error is precipitated by the stack being unwound with the aid of unhandled exceptions in your PLSQL code.

The options to resolve this Oracle error are:

Fix the circumstance that is inflicting the unhandled error. Write an exception handler for this unhandled error. Contact your DBA for help.

The ORA-06512 error message indicates the line variety of the unhandled error in the PLSQL code. This is pretty useful when troubleshooting.

Resolution

Option #1 – Fix the Error Condition

Let’s seem to be at an instance of how to get to the bottom of an ORA-06512 error by using fixing the error condition.

For example, if you created a manner referred to as TestProc as follows:

SQL> CREATE OR REPLACE PROCEDURE TestProc
  2  AS
  3    v_number number(2);
  4  BEGIN
  5    v_number := 100;
  6  END;
  7  /

Procedure created.

This technique used to be efficaciously created. But when we strive to execute this procedure, we will get an ORA-06512 error as follows:

SQL> execute TestProc();
BEGIN TestProc(); END;

*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: number precision too large
ORA-06512: at "EXAMPLE.TESTPROC", line 5
ORA-06512: at line 1

The first line of the error message (ie: ORA-06502) indicates the error that occurred, while the 2nd line of the error message (ie: ORA-06512) shows that the error came about at line 5 of the PLSQL code.

In this example, you have tried to assign a 3 digit quantity to a variable known as v_number that can solely handle two digits. You may want to right this error by way of redefining the v_number variable as number(3).

SQL> CREATE OR REPLACE PROCEDURE TestProc
  2  AS
  3    v_number number(3);
  4  BEGIN
  5    v_number := 100;
  6  END;
  7  /

Procedure created.

And now when we execute our TestProc procedure, the ORA-06512 error has been resolved.

SQL> execute TestProc();

PL/SQL procedure successfully completed.

Option #2 – Write an Exception Handler

Let’s look at an example of how to get to the bottom of an ORA-06512 error through writing an exception handler.

For example, if you created a system referred to as TestProc as follows:

SQL> CREATE OR REPLACE PROCEDURE TestProc
  2  AS
  3    v_number number(2);
  4  BEGIN
  5    v_number := 100;
  6  END;
  7  /

Procedure created.

This process used to be correctly created. But when we attempt to execute this procedure, we will get an ORA-06512 error as follows:

SQL> execute TestProc();
BEGIN TestProc(); END;

*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: number precision too large
ORA-06512: at "EXAMPLE.TESTPROC", line 5
ORA-06512: at line 1

The first line of the error message (ie: ORA-06502) suggests the error that occurred, while the 2nd line of the error message (ie: ORA-06512) suggests that the error took place at line 5 of the PLSQL code.

In this example, you’ve got tried to assign a three digit wide variety to a variable referred to as v_number that can only manage 2 digits. You could correct this by writing an exception handler to set the v_number variable to ninety nine (so that it is only two digits) when this error occurs.

SQL> CREATE OR REPLACE PROCEDURE TestProc
  2  AS
  3    v_number number(2);
  4  BEGIN
  5    v_number := 100;
  6 EXCEPTION
  7   WHEN OTHERS THEN
  8      v_number := 99;
  9 END;
 10 /

Procedure created.

And now when we execute our TestProc procedure, the ORA-06512 error has been resolved.

SQL> execute TestProc();

PL/SQL procedure successfully completed.