Introduction/ Issue:  

CSV blob file to raw file move from database table blob column to database directory.

Why we need to do / Cause of the issue: 

We can move from blob to raw CSV file from database table blob column to database directory.

How do we solve: 

Step 1: Create one database procedure

PROCEDURE P_FILE_TO_DBSER (p_blob      IN  BLOB,

p_dir       IN  VARCHAR2,

p_filename  IN  VARCHAR2)

AS

l_file      UTL_FILE.FILE_TYPE;

l_buffer    RAW(32767);

l_amount    BINARY_INTEGER := 32767;

l_pos       INTEGER := 1;

l_blob_len  INTEGER;

BEGIN

l_blob_len := DBMS_LOB.getlength(p_blob);

 

— Open the destination file.

l_file := UTL_FILE.fopen(p_dir, p_filename,’wb’, 32767);

— Read chunks of the BLOB and write them to the file until complete.

WHILE l_pos <= l_blob_len LOOP

DBMS_LOB.read(p_blob, l_amount, l_pos, l_buffer);

UTL_FILE.put_raw(l_file, l_buffer, TRUE);

l_pos := l_pos + l_amount;

END LOOP;

 

— Close the file.

UTL_FILE.fclose(l_file);

 

EXCEPTION

WHEN OTHERS THEN

— Close the file if something goes wrong.

IF UTL_FILE.is_open(l_file) THEN

UTL_FILE.fclose(l_file);

END IF;

Dbms_output.put_line(‘Error: ‘||SQLCODE||’-’||SQLERRM);

END;

Step 2: In oracle form builder create on button and write the below code in WHEN-BUTTON-PRESSED trigger.

DECLARE

v_seq       number;

l_blob          blob;

v_file_name varchar2(500) := ‘test_file.csv’;

BEGIN

SELECT FILE_UPLOAD_SEQ .CURRVAL INTO v_seq FROM DUAL;

BEGIN

SELECT file_data

INTO l_blob

FROM TB_FILE_UPLOAD

WHERE id = v_seq;

EXCEPTION

WHEN OTHERS THEN

l_blob := NULL;

END;

P_FILE_TO_DBSER ( p_blob       => l_blob,

p_dir        => ‘/db/oracle/file’,

p_filename   => v_file_name);

EXCEPTION

WHEN OTHERS THEN

MESSAGE(‘Error: ’||sqlerrm||’-’||sqlcode);

END;

Output:

After clicking button ‘test_file.csv’ raw file moved in this path ‘/db/oracle/file’ in database server.

Conclusion: 

We can store the csv raw file from blob column to database server path.

Recent Posts

Start typing and press Enter to search