I have an anonymous block that extract all Procedures for a database and then it writes each procedure in a file.
It work fine when I use utl_file.put_line (except that for each line is and space between each line) If I use utl_file.put the procedures are incomplete no exceptions
/*
create or replace directory plsql_path as '/u020/apps/klainet/plsql';
grant ALL on directory plsql_path to public;
select * from dba_directories;*/
DECLARE
tmp_name VARCHAR2(255) :='PROCEDURE';
v_path VARCHAR2(100) := '/u020/apps/klainet/plsql';
v_File_name VARCHAR2(100);
v_file UTL_FILE.FILE_TYPE;
v_flag BOOLEAN := FALSE;
str_len NUMBER;
loop_count NUMBER;
BEGIN
FOR line IN ( SELECT * FROM all_source WHERE OWNER NOT IN('SYS','SYSTEM') ORDER BY OWNER, TYPE,NAME, LINE) LOOP
IF tmp_name <> line.NAME THEN
IF v_flag = TRUE THEN
BEGIN
UTL_FILE.FCLOSE(v_file);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('error al escribir la linea '|| line.owner ||' ' || line.name);
END;
END IF;
tmp_name := line.NAME;
v_File_name := line.owner||'.'||line.NAME||'.sql';
--dbms_output.put_line(v_file_name);
BEGIN
v_file := UTL_FILE.FOPEN ('PLSQL_PATH', v_File_name, 'w');
v_File_name:='';
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('No se pudo crear el archivo de salida ' || v_File_name);
END;
v_flag := TRUE;
END IF;
BEGIN
--UTL_FILE.PUT(v_file, line.text);
str_len := LENGTH(line.text);
loop_count := 0;
WHILE loop_count < str_len
LOOP
SYS.utl_file.put_line(v_file, substr( line.text, loop_count +1, 255 ));
loop_count := loop_count +255;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
loop_count :=0;
DBMS_OUTPUT.PUT_LINE('error al escribir la linea '|| line.owner ||' ' || line.name);
END;
END LOOP;
END;
I don't fine any diference between put and put_line, except the space
UTL_FILE.PUT_LINE
will flush output to the physical file. UTL_FILE.PUT
won't.
If you want to use PUT
you need to flush the file manually; thus, replace fFile.PUT_LINE('whatever');
with fFile.PUT('whatever'); fFile.FFLUSH;
.
Best of luck.