-2

good afternoon, I am in need of help to read a field in the Oracle database. I have a long raw field that contains a pdf and I need to save it to a file. Does anyone have any suggestions on how I can do it?

jarlh
  • 42,561
  • 8
  • 45
  • 63

1 Answers1

0

If you have access to Oracle Support, there is an example in Doc ID 1433573.1

Sample Procedure

CREATE OR REPLACE PROCEDURE WritePDFToFILE (myfilename IN VARCHAR2)
IS
    v_blob          BLOB;
    blob_length     INTEGER;
    out_file        UTL_FILE.FILE_TYPE;
    -- chunk size and buffer size must match
    chunk_size      BINARY_INTEGER := 32767;
    v_buffer        RAW (32767);
    blob_position   INTEGER := 1;
BEGIN
    -- Retrieve the BLOB for reading
    SELECT filedata
      INTO v_blob
      FROM blob_pdf_tbl
     WHERE filename = myfilename;

    -- Retrieve the SIZE of the BLOB
    blob_length := DBMS_LOB.GETLENGTH (v_blob);

    -- Open a handle to the location where you are going to write the BLOB to a file
    -- NOTE: The 'wb' parameter means "write in byte mode" and is only availabe
    --       in the UTL_FILE package with Oracle 10g or later
    out_file :=
        UTL_FILE.FOPEN ('PDF_DIR_LOC',
                        myfilename,
                        'wb',
                        chunk_size);

    -- Write the BLOB to file in chunks
    WHILE blob_position <= blob_length
    LOOP
        IF blob_position + chunk_size - 1 > blob_length
        THEN
            chunk_size := blob_length - blob_position + 1;
        END IF;

        DBMS_LOB.READ (v_blob,
                       chunk_size,
                       blob_position,
                       v_buffer);
        UTL_FILE.PUT_RAW (out_file, v_buffer, TRUE);
        blob_position := blob_position + chunk_size;
    END LOOP;

    -- Close the file handle
    UTL_FILE.FCLOSE (out_file);
END;
/
EJ Egyed
  • 5,791
  • 1
  • 8
  • 23
  • thanks EJ, I tried using this solution before, but I got this feedback BEGIN WritePDFToFILE (36); END; Error reporting - ORA-00932: inconsistent data types: expected BLOB obtained LONG BINARY ORA-06512: in "TASY.WRITEPDFTOFILE", line 12 ORA-06512: online 1 00932. 00000 - "inconsistent datatypes: expected% s got% s" – Edinei Perazzoli Sep 23 '20 at 11:13