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 PHOTO
INTO l_blob
FROM TB_SUCCESS
WHERE IDENTITY_ID = IDENTITYID;
l_blob_len := DBMS_LOB.GETLENGTH(l_blob);
l_file := UTL_FILE.FOPEN('BLOBDIR',filename,'wb', l_blob_len);
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 directory BLOBDIR as 'd:\1000‘;
注意是在数据库服务器上建文件夹

CREATE ANY DIRECTORY to qingdao

create or replace procedure out_put_photo is
IDENTITYID varchar2(300);
cursor cur is
select IDENTITY_ID from TB_SUCCESS where id > 500;
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.IDENTITY_ID;
  photo_dump(identityid => IDENTITYID,
              filename => IDENTITYID||'.jpg');
end loop;
close cur;
end out_put_photo;

你可能感兴趣的:(oracle)