oracle存储过程将blob图片导出到文件

create or replace procedure photo_dump(IDENTITYID in varchar2,filename in varchar2) is
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
SELECT f_file
INTO l_blob
FROM trip_attach.t_photo
WHERE f_id = IDENTITYID;
l_blob_len := DBMS_LOB.GETLENGTH(l_blob);
l_file := UTL_FILE.FOPEN('BLOBDIR',filename,'wb', /*l_blob_len*/32767);
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
dbms_output.put_line(SQLERRM);
IF UTL_FILE.IS_OPEN(l_file) THEN
UTL_FILE.FCLOSE(l_file);
END IF;
RAISE;
end photo_dump;

create or replace procedure out_put_photo is
IDENTITYID varchar2(300);
cursor cur is
select f_xphoto_id from t_examinee_info where f_state='2' and f_exam_id=582;
cur_result cur%rowtype;
begin
if cur%isopen = false then
open cur;
end if;
loop
fetch cur into cur_result;
exit when cur%notfound;
IDENTITYID:=cur_result.f_xphoto_id;
  photo_dump(identityid => IDENTITYID,
              filename => IDENTITYID||'.jpg');
end loop;
close cur;
end out_put_photo;

create or replace directory BLOBDIR as 'c:\1000';
注意是在数据库服务器上建文件夹
grant CREATE ANY DIRECTORY to trip_biz;

猜你喜欢

转载自yuyunyan1123.iteye.com/blog/2020312