一、将/home/oracle/picture目录下的文件放入blob_content表的blob_column字段中:
grant create any directory to u02; --使用sys用户授权给u02用户创建目录的权限
conn u02/u02
create table blob_content(id int primary key,blob_column blob); --创建测试用表
create or replace directory images as '/home/oracle/picture'; --创建目录
create or replace procedure lob_insert(id int,filename varchar2) as
f_lob bfile;
b_lob blob;
begin
insert into BLOB_CONTENT(id,blob_column) values(id,empty_blob ()) return blob_column into b_lob;
f_lob:= bfilename('IMAGES',filename);
dbms_lob.fileopen(f_lob,dbms_lob.file_readonly);
dbms_lob.loadfromfile(b_lob,f_lob,dbms_lob.getlength(f_lob));
dbms_lob.fileclose(f_lob);
commit;
end;
/
SQL> call lob_insert(1,'001.jpg');
SQL> call lob_insert(2,'Koala.jpg');
SQL> call lob_insert(3,'234968.zip');
二、将blob_content的blob_column列内容输出到/home/oracle/out_file目录下:
SQL> create or replace directory out_file as '/home/oracle/out_file';
declare
l_file UTL_FILE.FILE_TYPE;
l_amount BINARY_INTEGER:=32767;
l_buffer RAW(32767);
l_pos NUMBER:=1;
l_Blob Blob;
l_Blob_len Number;
l_lob_id number;
CURSOR cur is
select id from BLOB_CONTENT;
BEGIN
open cur;
loop
fetch cur into l_lob_id;
EXIT when cur%NOTFOUND;
select BLOB_COLUMN into l_Blob from BLOB_CONTENT where id=l_lob_id;
l_file:=UTL_FILE.FOPEN('OUT_FILE','adapter_blob_'||to_char(l_lob_id)||'.bin','wb',32767); --此处生成文件名设置为.bin后缀,可根据实际情况修改
l_Blob_len:=dbms_lob.getlength(l_Blob);
l_pos:=1;
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;
utl_file.fclose(l_file);
end loop;
exception
when others then
if utl_file.is_open(l_file) then
utl_file.fclose(l_file);
end if;
END;
/
附:单独导出指定的一条数据中的lob字段的内容:
declare
l_file UTL_FILE.FILE_TYPE;
l_amount BINARY_INTEGER:=32767;
l_buffer RAW(32767);
l_pos NUMBER:=1;
l_Blob Blob;
l_Blob_len Number;
BEGIN
select BLOB_COLUMN into l_Blob from BLOB_CONTENT where id=10;
l_file:=UTL_FILE.FOPEN('OUT_FILE','001.jpg','wb',32767);
l_Blob_len:=dbms_lob.getlength(l_Blob);
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;
utl_file.fclose(l_file);
exception
when others then
if utl_file.is_open(l_file) then
utl_file.fclose(l_file);
end if;
END;
/
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30373263/viewspace-2123808/
,如需转载,请注明出处,否则将追究法律责任。