Question: I have a table column that is used
to store a jpg image. How do I extract a jpg image file from
inside an Oracle table?
Answer: You can use Oracle BLOB data types to
store photographs and images in jpg, gif, tif, eps, mov, mp3 or
any other format that you desire, it does not matter to a large binary
column. However, you cannot use standard SQL to retrieve an image
from an Oracle table, you need a PL/SQL procedure, like this one.
For more information on PL/SQL tips and tricks, I recommend Dr.
Hall's book "PL/SQL Tuning Secrets", a great source of working PL/SQL
code examples:
Retrieve a jpg photograph from Oracle:
DECLARE
t_blob BLOB;
t_len NUMBER;
t_file_name VARCHAR2(100);
t_output UTL_FILE.file_type;
t_TotalSize number;
t_position number := 1;
t_chucklen NUMBER := 4096;
t_chuck raw(4096);
t_remain number;
BEGIN
-- Get length of blob
SELECT DBMS_LOB.getlength (PHOTO), ename ||
'_1.jpg'
INTO t_TotalSize,
t_file_name FROM DEMO WHERE ENAME ='moon';
t_remain := t_TotalSize;
-- The directory TEMPDIR should exist before
executing
t_output :=
UTL_FILE.fopen ('TEMPDIR', t_file_name, 'wb', 32760);
-- Get BLOB
SELECT PHOTO INTO t_blob FROM DEMO WHERE ENAME
='moon';
-- Retrieving
BLOB
WHILE t_position <
t_TotalSize
LOOP
DBMS_LOB.READ (t_blob, t_chucklen, t_position,
t_chuck);
UTL_FILE.put_raw
(t_output, t_chuck);
UTL_FILE.fflush (t_output);
t_position := t_position + t_chucklen;
t_remain := t_remain - t_chucklen;
IF t_remain < 4096
THEN
t_chucklen := t_remain;
END IF;
END LOOP;
END;