-- define the directory inside Oracle when logged on as SYS
CREATE OR REPLACE DIRECTORY BLOB_DIR AS 'F:\LOB_IMAGES';
-- grant read on the directory to the hr schema
GRANT READ ON DIRECTORY BLOB_DIR TO hr; -- directory name
--- create lob_demo in hr schema
-- the storage table for the image file
CREATE TABLE lob_demo (
file_name VARCHAR2(30),
image BLOB ) -- image file
tablespace lobs;
-- create the procedure to load the file
CREATE OR REPLACE PROCEDURE load_file (fname IN VARCHAR2) IS
src_file BFILE;
dst_file BLOB;
lgh_file BINARY_INTEGER;
BEGIN
src_file := bfilename('BLOB_DIR', fname);
-- insert a NULL record to lock
INSERT INTO lob_demo -- table name is lob_demo
(file_name, image)
VALUES
(fname, EMPTY_BLOB())
RETURNING image INTO dst_file;
-- lock record
SELECT image
INTO dst_file
FROM lob_demo
WHERE file_name = fname
FOR UPDATE;
-- open the file
dbms_lob.fileopen(src_file, dbms_lob.file_readonly);
-- determine size
lgh_file := dbms_lob.getlength(src_file);
-- read the file
dbms_lob.loadfromfile(dst_file, src_file, lgh_file);
-- update the blob field
UPDATE lob_demo
SET image = dst_file
WHERE file_name = fname;
-- close file
dbms_lob.fileclose(src_file);
COMMIT;
END load_file;
------------------------------------------------------------------------------
sql > exec load_file('LOGO.GIF');
---- know the total length of image in bytes stored in oracle
sql > SELECT dbms_lob.getlength(IMAGE ) FROM LOB_DEMO;
No comments:
Post a Comment