Skip to content

Instantly share code, notes, and snippets.

@jpukg
Forked from jronal/export_blob.sql
Created October 10, 2016 18:43
Show Gist options
  • Save jpukg/0ad922b409678605333114c27a83dda0 to your computer and use it in GitHub Desktop.
Save jpukg/0ad922b409678605333114c27a83dda0 to your computer and use it in GitHub Desktop.
export BLOB oracle
DECLARE
l_file UTL_FILE.FILE_TYPE;
l_buffer RAW(32767);
l_amount BINARY_INTEGER := 32767;
l_pos INTEGER := 1;
l_blob BLOB;
l_blob_len INTEGER;
BEGIN
-- Get LOB locator
SELECT column2
INTO l_blob
FROM table1
WHERE COLUMN1 = 3;
l_blob_len := DBMS_LOB.getlength(l_blob);
-- Open the destination file.
l_file := UTL_FILE.fopen('BLOBS','mypdf3.pdf','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(l_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;
RAISE;*/
DBMS_OUTPUT.PUT_LINE('error');
dbms_output.put_line ('1 SQLCODE= '||SQLCODE);
dbms_output.put_line ('1 SQLERRM= '||SQLERRM);
END;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment