![](https://oss-emcsprod-public.modb.pro/modb_imgs/common/user.png)
oracle 表中的 blob 存放的 jpg 图片,如何导出来。 PL/SQL 中查询,不能导出,有其它方法吗(非 业务程序里导出)?
![](https://oss-emcsprod-public.oss-cn-beijing.aliyuncs.com/image/dbask.jpg)
要写一段存储过程。比较麻烦。
你看能借鉴多少。
create or replace procedure dd (newid number)
as
blob_loc blob;
handle utl_file.file_type;
pic_length int;
amount int;
offset int:=1;
buffer raw(30000);
temp int;
newname varchar2(40);
begin
select name,pic into newname,blob_loc from sskb where id=newid;
pic_length:=dbms_lob.getlength(blob_loc);
handle:=utl_file.fopen('USER_DIR',newname||'.jpg','wb');
temp:=trunc((pic_length-1)/1000)+1;
for i in 1..temp
loop
if i!=temp
then amount:=1000;
else
amount:=pic_length-(temp-1)*1000;
end if;
dbms_lob.read(blob_loc,amount,offset,buffer);
offset:=offset+1000;
utl_file.put_raw(handle,buffer);
end loop;
utl_file.fclose(handle);
end;
/
![](https://oss-emcsprod-public.modb.pro/modb_imgs/issue/issue-comment.png)
![](https://js-cdn.modb.cc/image/svgs/give-like.png)
你是想导出到数据库服务器上还是导出到客户端本地?
1.如果是导出到数据库上,用下面这个过程将BLOB转换成文件
procedure blob2file
( p_blob blob
, p_directory varchar2 := 'MY_DIR'
, p_filename varchar2 := 'my.jpg'
t_fh utl_file.file_type;
t_len pls_integer := 32767;
begin
t_fh := utl_file.fopen( p_directory
, p_filename
, 'wb'
for i in 0 .. trunc( ( dbms_lob.getlength( p_blob ) - 1 ) / t_len )
utl_file.put_raw( t_fh
, dbms_lob.substr( p_blob
, t_len
, i * t_len + 1
end loop;
utl_file.fclose( t_fh );