简体   繁体   中英

Substr with more than 4000 characters gets ORA-06502: PL/SQL: numeric or value error

In the below script if I try to substr for 4000 character it works and displays all text in my particular ID with respective DB field ID and Language, if I increase it even 4001 db pops up the error - ora-06502: pl/sql: numeric or value error.

Create or replace function GET_AER_TEXT5(M_AER_ID IN NUMBER,
  F_ID IN VARCHAR2,PREF_LANGUAGE IN VARCHAR2)
IS
  AERTEXT VARCHAR2(32000);
  LANG_PARAM VARCHAR2(2000);
  AER_CLOB CLOB;
BEGIN
  FOR c1 IN (
    select TEXT from AER_TEXT
    where FIELD_ID=F_ID and AER_ID=M_AER_ID and LANGUAGE IN(PREF_LANGUAGE)
  )
  LOOP
    IF c1.text IS NOT NULL THEN
      AER_CLOB:=AER_CLOB || c1.text;
    END IF;
  END LOOP;
  AERTEXT:=substr(AER_CLOB,1,4000);
  RETURN(AERTEXT);
END;

Importance of increasing this to more than 4000 is to pull complete text data. If the DB column contains more than 4K character it doesn't work.

I'm calling it with:

select AER_ID,GET_AER_TEXT5(AER_ID,at,field_id,'001')
from AER a,AER_TEXT AT
where AT.field_ID=12345 and a.aer_id=at.aer_id;

Can you please advise how to get rid of this issue.

Prior to Oracle 12c Oracle only allows 4000 bytes in a varchar2 value in an SQL context. You are allowed 32k in PL/SQL, so your function is sort of OK as it stands, even with the substrng getting the first 4001 characters; but only if you call it from PL/SQL. When you try to call it from SQL:

select AER_ID,GET_AER_TEXT5(AER_ID,at,field_id,'001') ...

... you're trying to assign a 4001-character value to the implicit SQL column in the return statement, and that is causing the error you are seeing.

You can either change your SAP call to use a PL/SQL context and a bind variable to get the return value, though you'll still be limited to 32k; or change your function to keep value as a CLOB, which makes the function a bit pointless as you can just get the value from the table. I'm not familiar with SAP so I'm not quite sure how you'd end up coding either approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM