Question : ORA-06502 When using MAX(Column) with %TYPE

Hi,

I have tried to narrow down the problem as much as I can, and have managed to isolate it to a particular scenario. As the structure and data is sensitive, I have had to come up with a scenario which I have tested and it causes the same issue to occur. The scenario is as follows

Assuming I have a table defined as follows

CREATE TABLE TEST_TABLE   (TEST_COLUMN CHAR(8 BYTE)) ;

and the table only has a single record NZ07100S

We also define a function as

CREATE OR REPLACE FUNCTION
FUNCTION GETTESTVALUE
RETURN TEST_TABLE.TEST_COLUMN%TYPE
IS
  TEST_VALUE TEST_TABLE.TEST_COLUMN%TYPE;
BEGIN
  SELECT MAX(TEST_COLUMN) INTO TEST_VALUE FROM TEST_TABLE;
  RETURN TEST_VALUE;
END;



We ran the following command

SELECT GETTESTVALUE FROM DUAL;

and receive an error as follows

Error report:
SQL Error: ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at "GETTESTVALUE", line 6
06502. 00000 -  "PL/SQL: numeric or value error%s"


However, if were to modify the function to the following

CREATE OR REPLACE FUNCTION
FUNCTION GETTESTVALUE
RETURN TEST_TABLE.TEST_COLUMN%TYPE
IS
  TEST_VALUE TEST_TABLE.TEST_COLUMN%TYPE;
BEGIN
  SELECT TEST_COLUMN INTO TEST_VALUE FROM TEST_TABLE;
  RETURN TEST_VALUE;
END;

There is no error reported and the value NZ07100S is returned.

Of course, when we modified the function to be

CREATE OR REPLACE FUNCTION
FUNCTION GETTESTVALUE
RETURN TEST_TABLE.TEST_COLUMN%TYPE
IS
  TEST_VALUE TEST_TABLE.TEST_COLUMN%TYPE;
BEGIN
  SELECT CAST(MAX(TEST_COLUMN) AS CHAR(8))INTO TEST_VALUE FROM TEST_TABLE;
  RETURN TEST_VALUE;
END;

No errors are reported, and the value is returned

However, the function in question has been in production usage since 2004. We are currently making some modifications to the application and had imported the backup from Oracle 10g into Oracle XE. When we try to run the function, the error is thrown out.

I would like to understand if this is a limitation with Oracle XE that causes this problem. It would seem to me that the problem is selecting the MAX value into the variable that had been defined. Running the query directly from sqlplus does not generate an error, and as such do not feel there is an error with the SQL. The only time we seem to get the error is with the INTO statement.

Is there are a means to use the function as it has been defined in production? Why would the ORA-06502 error be thrown out? It does not seem to make sense to me. Would appreciate any guidance on this.


Thanks and regards

Jega


 

Answer : ORA-06502 When using MAX(Column) with %TYPE

Hi,

Having the same discussion on OTN. The output from sqlplus is as follows

SQL> select * from v$version;
 
BANNER
----------------------------------------------------------------
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product
PL/SQL Release 10.2.0.1.0 - Production
CORE    10.2.0.1.0      Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
 
SQL> create table test_table (test_column char(8 byte));
 
Table created.
 
SQL> insert into test_table
  2       values ('NZ07100S');
 
1 row created.
 
SQL> commit
  2  /
 
Commit complete.
 
SQL> create or replace function gettestvalue
  2      return test_table.test_column%type
  3  is
  4     test_value   test_table.test_column%type;
  5  begin
  6     select max(test_column) into test_value from test_table;
  7  return test_value;
  8  end;
  9  /
 
Function created.
 
SQL> select gettestvalue from dual
  2  /
select gettestvalue from dual
       *
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at "JEGA.GETTESTVALUE", line 6
 
 
SQL> spool off

Also, the output from the length of the values is as follows

SQL> select lengthb (TEST_COLUMN)
  2  from test_table
  3  where test_column = 'NZ07100S'
  4  /
 
LENGTHB(TEST_COLUMN)
--------------------
                   8
 
SQL> select max (lengthb (test_column))
  2  from test_table
  3  /
 
MAX(LENGTHB(TEST_COLUMN))
-------------------------
                        8

A couple of the users on OTN have also been testing this case, and so far we have not noticed it on Oracle Enterprise Edition. Changing the data type to varchar(8) does seem to solve the problem.

SQL> alter table test_table modify test_column varchar2 (8 byte);
 
Table altered.
 
SQL> select gettestvalue from dual;
 
GETTESTVALUE
--------------------------------------------------------------------------------
NZ07100S
 
SQL> spool off;


It does seem to be a problem that is unique to XE. Still trying to figure out if this is a known issue. Since XE is used by the developers for development and testing purposes, I guess we should be alright as the Production Database is running Enterprise edition. However, it does seem odd for this problem to occur
Random Solutions  
 
programming4us programming4us