1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| CREATE OR REPLACE PROCEDURE Write_Binary_File
(
PC$Directory IN VARCHAR2
,PC$File_Name IN VARCHAR2
,PC$SQL_Order IN VARCHAR2
,PB$Raise IN BOOLEAN DEFAULT FALSE
)
-- -------------------------------------------
-- Procedure to dump a BLOB column onto a file
-- -------------------------------------------
-- parameters:
-- PC$Directory : name of an existing Oracle Directory
-- PC$File_Name : name of the expected output file
-- PC$SQL_Order : SQL order to select the BLOB column
-- PB$Raise : boolean to indicate if the process
-- would be stopped after an error
--
IS
src_lob BLOB;
buffer RAW(16384);
amt BINARY_INTEGER := 16384;
pos INTEGER := 1;
LF$FicOUT UTL_FILE.FILE_TYPE ;
LC$Msg VARCHAR2(2000) ;
BEGIN
-- get the BLOB column --
BEGIN
EXECUTE IMMEDIATE PC$SQL_Order INTO src_lob ;
EXCEPTION
WHEN NO_DATA_FOUND THEN
LC$Msg := 'Write_Binary_File(): NO_DATA_FOUND' ;
IF PB$Raise THEN
RAISE_APPLICATION_ERROR( -20100, LC$Msg ) ;
END IF ;
DBMS_OUTPUT.PUT_LINE(LC$Msg) ;
RETURN ;
END ;
-- open the output file --
LF$FicOUT := UTL_FILE.FOPEN( PC$Directory, PC$File_Name, 'W', 32764 ) ;
-- write the file --
LOOP
-- read the chunks --
Dbms_Lob.READ (src_lob, amt, pos, buffer);
-- write the chunks --
UTL_FILE.Put_Raw(LF$FicOut, buffer);
pos := pos + amt;
END LOOP;
-- close the file --
UTL_FILE.FCLOSE(LF$FicOut);
EXCEPTION
WHEN NO_DATA_FOUND THEN
UTL_FILE.FCLOSE(LF$FicOut);
WHEN OTHERS THEN
LC$Msg := 'Write_Binary_File() Error : ' || TO_CHAR( SQLCODE ) || ' while managing file ('
|| PC$Directory || ') ' || PC$File_Name ;
IF PB$Raise THEN
RAISE_APPLICATION_ERROR( -20100, LC$Msg ) ;
END IF ;
DBMS_OUTPUT.PUT_LINE(LC$Msg);
END Write_Binary_File;
/ |
Partager