views:

153

answers:

1

I have the following function:

CREATE OR REPLACE FUNCTION GetVarchar2 (iclCLOB IN Nvarchar2)
return NVARCHAR2
IS
cnuMAX_LENGTH Constant number := 32767 ;
nuLength Number := DBMS_LOB.getlength(iclCLOB);
sbBuffer Nvarchar2(32767);
begin
dbms_lob.read(iclCLOB,nuLength,1,sbBuffer);
return sbBuffer;
END;

when I call it like this:

select GetVarChar2(text) from posts where postid = 'anId';

I get this error:

ORA-22835: Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual: 6058, maximum: 2000)
22835. 00000 - "Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual: %s, maximum: %s)"
*Cause: An attempt was made to convert CLOB to CHAR or BLOB to RAW, where
the LOB size was bigger than the buffer limit for CHAR and RAW
types.
Note that widths are reported in characters if character length semantics are in effect for the column, otherwise widths are reported in bytes.
*Action: Do one of the following
1. Make the LOB smaller before performing the conversion,
for example, by using SUBSTR on CLOB
2. Use DBMS_LOB.SUBSTR to convert CLOB to CHAR or BLOB to RAW.

The problem is that the size of text in posts table in of type NCLOB and is 6059 bytes. It's strange because when I do this without calling function it runs well. i.e. when I run the following script:

declare 
    iclCLOB nvarchar2(6100) := 'Here is the same 6059 bytes string';
    cnuMAX_LENGTH number := 32767 ;
    nuLength Number := DBMS_LOB.getlength(iclCLOB);
    sbBuffer Nvarchar2(32767);
    sbBuffer1 Nvarchar2(32767);
begin
    dbms_lob.read(iclCLOB,nuLength,1,sbBuffer);
    select GetVarChar2(text) into sbBuffer1 from posts where postid = 'anId';
end;

It runs without any problems.

Thank you.

A: 

NVARCHAR2 can be 32767 bytes in PL/SQL but only 4000 bytes in SQL. Also, try changing the parameter iclCLOB to a NCLOB instead of NVARCHAR2 - the implicit conversion will cause problems.

Jon