What is "ORA-06502 PL/SQL: numeric or value error"?
This is a very generic error and there can be any number of reasons for this error. In most cases, the error is due to a PL/SQL code issue, like mismatch in variable data type and the value being assigned to it or assigning more character value to a VARCHAR or CHAR variable than it can hold etc. The root cause is often specific to what the code is doing at the time of the error and in majority of the cases the problem is due to the PL/SQL code.
How to troubleshoot the "ORA-06502 PL/SQL: numeric or value error"?
The first steps in troubleshooting the ORA-06502 error is to identify the failing PL/SQL statement. Oracle provides an event tracing facility that can be used to identify the failing PL/SQL statement.
Enable the trace as below:
ALTER SYSTEM SET EVENTS '6502 TRACE NAME ERRORSTACK LEVEL 3';
This event trace can also be enabled at the session level. Enable the trace at the session level if the error can be reproduced by running the failing PL/SQL code from a SQL*Plus session.When enabling the trace at session level, you can also give a naming pattern for the trace to identify the trace file easily, this is done using:
ALTER SESSION SET TRACEFILE_IDENTIFIER='6502_TRACE';
This will generate a trace file with name "<SID>_ora_<PID>_6502_TRACE.trc" so that the trace can be identified easily.
Once the event tracing is enabled, run the failing PL/SQL code to generate a trace file. Once the trace is generated successfully, disable the trace as below:
ALTER SYSTEM SET EVENTS '6502 TRACE NAME CONTEXT OFF';
Review the trace file to identify the line of the PL/SQL code that fails with ORA-06502 error.For example, the below code when run will fail with ORA-0652 trace.
SET SERVEROUTPUT ON
DECLARE
v_testvar VARCHAR2(10);
BEGIN
v_testvar := 'ABCDEFGHIJKL';
END;
/
And the trace file will show below call stack.
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
----- Current SQL Statement for this session (sql_id=163u4nvy76u8r) -----
DECLARE
v_testvar VARCHAR2(10);
BEGIN
v_testvar := 'ABCDEFGHIJKL';
END;
----- PL/SQL Stack -----
----- PL/SQL Call Stack -----
object line object
handle number name
0x64308080 4 anonymous block
The call stack shows that there is a problem in line 4 of the anonymous block.
Examine the line of the code that the call stack is showing and analyze what the code is trying to do at the time of the error. Based on this further debugging may be required.
The most common root cause is, when there is a mismatch between the data type of the variable and the value that is being assigned to it.
For Example:
It could be either a direct value assignment (E.g.) var_name := <value> or while fetching data from table to the variable or while getting the data as result of another PL/SQL call through parameters etc.
Mismatch between the client side NLS_LANG and NLS_CHARACTERSET of the database.
The issue also can occur when there is a mismatch between client side character set NLS_LANG and the database character set NLS_CHARACTERSET. When working with UNICODE database (UTF8/AL32UTF8), make sure that:
Some common code related issues are shown below:
SET SERVEROUTPUT ON
DECLARE
V_VAR1 VARCHAR2(10);
V_VAR2 NUMBER;
BEGIN
V_VAR2 := 'MIKE'; /* VALID ORA-6502 */
V_VAR1 := 'ABCDEFGHIKL'; /* VALID ORA-6502 */
END;
/
SET SERVEROUTPUT ON
DECLARE
V_VAR1 VARCHAR2(3);
BEGIN
SELECT 'ABCD' INTO V_VAR1 FROM DUAL;
END;
/
CREATE OR REPLACE FUNCTION
TEST_6502_ERROR
RETURN VARCHAR2
AS
BEGIN
RETURN 'ASDF';
END;
/
SET SERVEROUTPUT ON
DECLARE
V_GET_VALUE VARCHAR2(3);
BEGIN
V_GET_VALUE := TEST_6502_ERROR;
END;
/