Avatar of Kodarc
Kodarc
 asked on

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


 
Oracle Database

Avatar of undefined
Last Comment
Kodarc

8/22/2022 - Mon
Ivo Stoykov

Hi Kodarc
1st, char is not recommended type, user varchar2(8) instead.
2nd I do not see any insert. try to use select * from TEST_TABLE; and see what is in there.
char type is trying to fill all length size so there migh be problems there=
3rd MAX is a numeric function basically. Applied on char might bring unexpected results
HTH
Ivo Stoykov
Ora_Techie

Here is the test case:
CREATE TABLE TEST_TABLE   (TEST_COLUMN CHAR(8 BYTE));
insert into test_table (TEST_COLUMN) values('NZ07100S');
CREATE OR REPLACE 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;

SQL> SELECT GETTESTVALUE FROM DUAL;
GETTESTVALUE
-----------------------------------
NZ07100S
 SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - Prod
PL/SQL Release 10.2.0.2.0 - Production
CORE    10.2.0.2.0      Production
TNS for Linux: Version 10.2.0.2.0 - Production
NLSRTL Version 10.2.0.2.0 - Production
It is working fine for me. Can you please post a test case?
ASKER CERTIFIED SOLUTION
Kodarc

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck