Error- ORA-22835: Buffer too small for CLOB to CHAR or BLOB to RAW conversion
Asked Answered
S

3

18

I am attempting to read a BLOB message and display it as a variable in one of my procedures, but am getting the error below:

Error - ORA-22835: Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual: 9923, maximum: 2000)

I have googled and found a suggestion to trim the BLOB message as below, but I would rather display the complete BLOB message as a string.

UTL_RAW.CAST_TO_VARCHAR2(dbms_lob.substr(variable_name,2000,1)) 

How can I display the complete message? Is there a setting on either the database or procedure level that I can change?

Starwort answered 27/8, 2014 at 4:8 Comment(0)
S
2

I got this working by using the option described here:

https://web.archive.org/web/20140702173209/http://www.dba-oracle.com/t_convert_blob_varchar_datatype.htm

Starwort answered 27/8, 2014 at 9:16 Comment(2)
Could you explain how to insert it?Yurev
Link does not work for me. It would be helpful to summarize the solution.Crinkleroot
I
3

You can get around the buffer issue by reading the BLOB value in chunks. In order to do that, you can define a user defined function (UDF) as suggested here. The following UDF reads the BLOB field one chunk at a time, converts that to VARCHAR2 and appends the results consecutively to return the result as a CLOB:

CREATE OR REPLACE FUNCTION VC2CLOB_FROM_BLOB(B BLOB)
RETURN CLOB IS
    c CLOB;
    n NUMBER;
BEGIN
    IF (b IS NULL) THEN 
        RETURN NULL;
    END IF;
    IF (LENGTH(b) = 0) THEN
        RETURN EMPTY_CLOB();
    END IF;
    DBMS_LOB.CREATETEMPORARY(c, TRUE);
    n := 1;
    WHILE (n + 32767 <= LENGTH(b)) LOOP
        DBMS_LOB.WRITEAPPEND(c, 32767, UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(b, 32767, n)));
        n := n + 32767;
    END LOOP;
    DBMS_LOB.WRITEAPPEND(c, LENGTH(b) - n + 1, UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(b, LENGTH(b) - n + 1, n)));
    RETURN c;
END;
/

After having defined it, you can simply call it like so:

SELECT VC2CLOB_FROM_BLOB(variable_name);

Worked like a charm for my problem.

Indurate answered 14/1, 2022 at 20:37 Comment(1)
This may help, but there's a better solution available.Eddra
S
2

I got this working by using the option described here:

https://web.archive.org/web/20140702173209/http://www.dba-oracle.com/t_convert_blob_varchar_datatype.htm

Starwort answered 27/8, 2014 at 9:16 Comment(2)
Could you explain how to insert it?Yurev
Link does not work for me. It would be helpful to summarize the solution.Crinkleroot
C
1

Try to use DBMS_LOB.GETLENGTH(), instead of trimming the string

Cream answered 30/1, 2018 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.