sql >> Base de Datos >  >> RDS >> Oracle

ERROR en la línea 191:ORA-01489:el resultado de la concatenación de cadenas es demasiado largo

VARCHAR2 están limitados a 4000 bytes. Si recibe este error

Entonces está bastante claro que la concatenación supera los 4000 bytes.

¿Ahora qué hacer?

Su primera solución para usar CLOB en su lugar es correcta.

select TO_CLOB(a)|| TO_CLOB(b)|| TO_CLOB(c) || TO_CLOB(d) 

Parece que su verdadero problema es guardar en un archivo

Si bien no publicó cómo guardar el clob resultante en un archivo, creo que no lo está haciendo correctamente. Si intenta guardar en un archivo de la misma manera que lo hacía con VARCHAR2, lo está haciendo mal.

Primero debe usar dbms_lob.read para leer el clob de la base de datos, luego use utl_file.put_raw para escribir en el archivo.

DECLARE
    position NUMBER := 1;
    byte_length NUMBER := 32760;
    length NUMBER;
    vblob BLOB;
    rawlob RAW(32760);
    temp NUMBER;
    output utl_file.file_type;
BEGIN
    -- Last parameter is maximum number of bytes returned.
    -- wb stands for write byte mode
    output := utl_file.fopen('DIR', 'filename', 'wb', 32760);

    position := 1;
    select dbms_lob.getlength(yourLob)
    into len
    from somewhere
    where something;

    temp := length;

    select yourLob
    into vlob
    from somewhere
    where something;

    IF len < 32760 THEN
        utl_file.put_raw(output, vblob);
        -- Don't forget to flush
        utl_file.fflush(output);
    ELSE -- write part by part
        WHILE position < len AND byte_length > 0
        LOOP
           dbms_lob.read(vblob, byte_length, position, rawlob);

           utl_file.put_raw(output,rawlob);

           -- You must admit, you would have forgot to flush.
           utl_file.fflush(output); 

           position := position + byte_length;

           -- set the end position if less than 32000 bytes
           temp := temp - bytelen;
           IF temp < 32760 THEN
               byte_length := temp;
           END IF;
    END IF;
END;