Utl_file help!!!
Hey people :) Below is my code it is produces a list of names address for a mail merge, an example of an outputted file:
32-36 GREAT VICTORIA STREET
BELFAST
BT2 7BA
08448920902
0026936
000001
MRS FRAME
51 HOUSE DRIVE
HOUSETOWN
COUNTY ANTRIM
BT36 7JY
03 DEC 2008
MRS FRAME
0.35
28-JUL-08
07-DEC-08
01-APR-08
07-DEC-08
0.72
ALAN CASKEY
Which is perfect, but when some users input the data, tehy leave the l.INPUT_HOUSE_NO as null and input the house number as part of the l.input_address_line1, which leaves a blank space where the house number should be for example:
32-36 GREAT VICTORIA STREET
BELFAST
BT2 7BA
08448920902
0031955
000002
MR TOWNSLEY
__2 HOUSE GARDENS (unneeded space in the address, how could i fix this??)
HOUSETOWN
BT13 3RJ
03 DEC 2008
MR TOWNSLEY
0.04
01-APR-08
23-NOV-08
01-APR-08
23-NOV-08
0.60
ALAN CASKEY
Thanks
DECLARE
v_count NUMBER(10) := 0;
wk_start_seqno date;
EXEC_file UTL_FILE.FILE_TYPE;
CURSOR read_lpa_input
IS
SELECT
a.address_line3,
a.address_line4,
a.address_line5,
a.post_code,
a.tel_no,
l.claim_no,
lpad(row_number() OVER(ORDER BY l.claim_no), 6, '0') AS snum,
l.input_title,
l.input_surname,
l.INPUT_HOUSE_NO,
l.input_address_line1,
l.input_address_line2,
l.input_address_line3,
l.input_address_line4,
l.input_post_code,
to_char(sysdate, 'DD MON YYYY') AS prntdate,
l.input_title AS input_title2,
l.input_surname AS input_surname2,
h.lpa_amt,
h.start_date,
c.paidto_date,
h.start_date AS start_date2,
c.paidto_date AS paidto_date2,
c.overpay_halahra,
a.sho_name
FROM lpa_input l, lpa_address a, lpa_history h, lpa_claim c
WHERE a.aun_code = l.input_aun_code
AND h.claim_no = c.claim_no
AND l.claim_no = h.claim_no
AND c.overpay_halahra > '0'
and ent_seqno = (select max (ent_seqno) from lpa_history where claim_no = c.claim_no);
TYPE wk_tab IS TABLE OF read_lpa_input%ROWTYPE
INDEX BY PLS_INTEGER;
wk wk_tab;
BEGIN
exec_file := utl_file.fopen('/spp/spool/RBLIVE/rr_output', 'output501.txt', 'W');
OPEN read_lpa_input;
LOOP
EXIT WHEN read_lpa_input%NOTFOUND;
FETCH read_lpa_input
BULK COLLECT INTO wk LIMIT 100;
FOR i IN 1 .. wk.count
LOOP
select start_date into wk_start_seqno from lpa_history where claim_no = wk(i).claim_no
and ent_seqno = (select max (ent_seqno) - 1 from lpa_history
where claim_no = wk(i).claim_no);
v_count :=0;
utl_file.put_line(exec_file, wk(i).address_line3);
utl_file.put_line(exec_file, wk(i).address_line4);
IF wk(i).address_line5 IS NOT NULL THEN
utl_file.put_line(exec_file, wk(i).address_line5);
ELSE
v_count := v_count+1; -- count the number of blank lines
END IF;
utl_file.put_line(exec_file, wk(i).post_code);
IF v_count > 0 THEN -- we have omitted some lines
FOR i IN 1..v_count LOOP -- loop that many times
utl_file.new_line(exec_file); -- write a blank line
END LOOP;
END IF;
utl_file.put_line(exec_file, wk(i).tel_no);
utl_file.put_line(exec_file, wk(i).claim_no);
utl_file.put_line(exec_file, wk(i).snum);
utl_file.put_line(exec_file, wk(i).input_title||' '||wk(i).input_surname);
v_count :=0;
---------------------------------------------------------------------------------HELP HERE!!!!
IF wk(i).input_address_line1 IS NOT NULL THEN
utl_file.put_line(exec_file, wk(i).INPUT_HOUSE_NO||' '||wk(i).input_address_line1);
ELSE
v_count := v_count+1; -- count the number of blank lines
END IF;
IF wk(i).input_address_line2 IS NOT NULL THEN
utl_file.put_line(exec_file, wk(i).input_address_line2);
ELSE
v_count := v_count+1; -- count the number of blank lines
END IF;
IF wk(i).input_address_line3 IS NOT NULL THEN
utl_file.put_line(exec_file, wk(i).input_address_line3);
ELSE
v_count := v_count+1; -- count the number of blank lines
END IF;
IF wk(i).input_address_line4 IS NOT NULL THEN
utl_file.put_line(exec_file, wk(i).input_address_line4);
ELSE
v_count := v_count+1; -- count the number of blank lines
END IF;
utl_file.put_line(exec_file, wk(i).input_post_code);
IF v_count > 0 THEN -- we have omitted some lines
FOR i IN 1..v_count LOOP -- loop that many times
utl_file.new_line(exec_file); -- write a blank line
END LOOP;
END IF;
utl_file.put_line(exec_file, wk(i).prntdate);
utl_file.put_line(exec_file, wk(i).input_title2||' '||wk(i).input_surname2);
utl_file.put_line(exec_file, to_char(round(wk(i).lpa_amt,2),'99G990D00'));
utl_file.put_line(exec_file, wk(i).start_date);
utl_file.put_line(exec_file, wk(i).paidto_date);
utl_file.put_line(exec_file, wk_start_seqno);
utl_file.put_line(exec_file, wk(i).paidto_date2);
utl_file.put_line(exec_file, to_char(round(wk(i).overpay_halahra,2),'99G990D00'));
utl_file.put_line(exec_file, wk(i).sho_name);
END LOOP;
END LOOP;
CLOSE read_lpa_input;
utl_file.fclose(exec_file);
END;
/Edited by: user653315 on 03-Dec-2008 04:11
Try this...
IF wk(i).input_address_line1 IS NOT NULL THEN
utl_file.put_line(exec_file, CASE WHEN wk(i).INPUT_HOUSE_NO IS NULL THEN NULL ELSE wk(i).INPUT_HOUSE_NO||' ' END||wk(i).input_address_line1);
ELSE
v_count := v_count+1; -- count the number of blank lines
END IF;
Similar Messages
-
UTL_FILE help..Plz Help urgently
CREATE OR REPLACE PROCEDURE test_file
AS
n_output_file_handle UTL_FILE.file_type;
/**************************Beginning of the procedure **********************/
BEGIN
--DBMS_OUTPUT.ENABLE(2000000);
DBMS_OUTPUT.put_line('p_utl_dir:');
DBMS_OUTPUT.put_line('c_output_file_name:'|| 'test.txt' );
n_output_file_handle := UTL_FILE.fopen ('C:\MPPI','test.txt','w');
UTL_FILE.put_line (n_output_file_handle, 'test1');
UTL_FILE.fclose(n_output_file_handle);
I have this procedure to write text to a file. I execute this on my own machine but it does not write to the file.I exceute it from SQLPlus by execute test_file();.It says proc successfully completed but nothing is written to text file.
Could somone plz help me with this urgently.Hi, what is the value for the parameter UTL_FILE?
You use an absolute path for the directory to write to in you r procedure.
If you would use a directory object in the database to specify where to write to you would not need to set UL_FILE parameter.
Regards,
Lutz -
UTl_file help - To generate table records using execute imediate statment
Hi Guys,
I need small help from you guys to generate file . For below results I would like to send it UTL_file and generate text file on table name . My table name will be dynamic ( suppose now emp table as input next record will be employee as input like so but my all columns are same for all the tables ( as inputs to this procedure) )
declare
type empdtlrec is record (empno number(4),
ename varchar2(20),
deptno number(2));
empdtl empdtlrec;
begin
execute immediate 'select empno, ename, deptno ' ||
'from emp where empno = 7934'
into empdtl;
end;
Please help me for the above querydeclare
type empdtlrec is record (empno number(4),
ename varchar2(20),
deptno number(2));
empdtl empdtlrec;
begin
execute immediate 'select empno, ename, deptno ' ||
'from emp where empno=(select max(empno) from emp)'
into empdtl;
dbms_output.put_line('empno ' || empdtl.empno );
dbms_output.put_line('ename ' || empdtl.ename );
dbms_output.put_line('deptno ' ||empdtl.deptno );
end;
I want to create a file using above records and send a mail to users .Please help me -
UTL_FILE help/samples
Hello Folks,
I need to write a routine to read in a CSV file parse it and inser the data into a table.
Does anybody have examples?
TIAUse this as a skeleton:
set serveroutput on size 1000000 format word_wrapped
set verify off
DECLARE
EOF CONSTANT VARCHAR2(30) := '~~+~~+~~';
iCount NUMBER;
sEOF VARCHAR2(30);
sPath VARCHAR2(2000);
sRecord VARCHAR2(2000);
sSQLCode NUMBER;
sSQLErrM VARCHAR2(255);
pFileHandle utl_file.file_type;
BEGIN
dbms_output.enable(1000000);
iCount := 0;
sEOF := '';
sPath := 'C:\Data';
pFileHandle := utl_file.fopen(sPath, 'file.cvs', 'r');
loop
BEGIN
utl_file.get_line(pFileHandle, sRecord);
EXCEPTION
when NO_DATA_FOUND then sEOF := EOF;
END;
exit when sEOF = EOF;
iCount := iCount + 1;
dbms_output.put_line(sRecord);
end loop;
commit;
dbms_output.put_line("Records read: "| |iCount);
utl_file.fclose(pFileHandle);
EXCEPTION
when UTL_FILE.INVALID_OPERATION then
utl_file.fclose(pFileHandle);
raise_application_error(-20001, 'Invalid Operation');
when UTL_FILE.INVALID_FILEHANDLE then
utl_file.fclose(pFileHandle);
raise_application_error(-20002, 'Invalid File Handle');
when UTL_FILE.WRITE_ERROR then
utl_file.fclose(pFileHandle);
raise_application_error(-20003, 'Write Error');
when UTL_FILE.INTERNAL_ERROR then
utl_file.fclose(pFileHandle);
raise_application_error(-20004, 'Internal Error');
when UTL_FILE.INVALID_PATH then
utl_file.fclose(pFileHandle);
raise_application_error(-20005, 'Invalid Path ['| |sPath| |']');
when UTL_FILE.INVALID_MODE then
utl_file.fclose(pFileHandle);
raise_application_error(-20006, 'Invalid Mode');
when UTL_FILE.READ_ERROR then
utl_file.fclose(pFileHandle);
raise_application_error(-20007, 'Read Error');
when OTHERS then
utl_file.fclose(pFileHandle);
sSQLCode := SQLCODE;
sSQLErrM := substr(SQLERRM, 1, 255);
raise_application_error(-20008, sSQLCode| |' - '| |sSQLErrM);
END;
To chop the record into separate fields you must use something like:
iStart := instr(sRecord, '|', 1, 2) + 1;
iLen := instr(sRecord, '|', 1, 3) - iStart;
if iLen = 0 then
sField := '#NULL#';
else
sField := substr(sRecord, iStart, iLen);
null -
Reg : txt Files in Server directory (UTL_FILE)
Hi Experts,
Suppose, I've placed 5 +.txt+ files (procedure/table creation scripts) in a db server directory.
Is there any way I can loop through each of the files?
I need to pull the content of each file into oracle table using a procedure. So, I'm trying dynamically trying to use External Tables with Execute Immediate inside my proc. Will package UTL_FILE help in this?
Please give some suggestions.
Let me know if you have any concerns.
Ranit B.As an alternative solution, have you considered using external LOB's? Using this approach could save significant overhead vs. the external tables solution because it doesn't require any schema objects for accessing the txt files (if you use an SQL script instead of a stored procedure).
http://docs.oracle.com/cd/B14117_01/appdev.101/b10796/adlob_bf.htm#1010878
Below is an excerpt from the link the Oracle docs above; this could be used as a starting point. (Substitute your directory name/file name for the BFILENAME arguments.)
/* This file is installed in the following path when you install */
/* the database: $ORACLE_HOME/rdbms/demo/lobs/plsql/fdisplay.sql */
/* Displaying BFILE data. */
/* Procedure displayBFILE_proc is not part of DBMS_LOB package: */
CREATE OR REPLACE PROCEDURE displayBFILE_proc IS
file_loc BFILE := BFILENAME('MEDIA_DIR', 'monitor_3060.txt');
Buffer RAW(1024);
Amount BINARY_INTEGER := 200;
Position INTEGER := 1;
BEGIN
DBMS_OUTPUT.PUT_LINE('------------ BFILE DISPLAY EXAMPLE ------------');
/* Opening the BFILE: */
DBMS_LOB.OPEN (file_loc, DBMS_LOB.LOB_READONLY);
LOOP
DBMS_LOB.READ (file_loc, Amount, Position, Buffer);
/* Display the buffer contents: */
DBMS_OUTPUT.PUT_LINE(substr(utl_raw.cast_to_varchar2(Buffer), 1, 250));
Position := Position + Amount;
END LOOP;
/* Closing the BFILE: */
DBMS_LOB.CLOSE (file_loc);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('End of data');
END;
SHOW ERRORS; -
Help needed in utl_file
Hi,
I have two groups in data model of my report. I am using utl_file in a formula column of report last group.it's working fine.
but my problem is if customer has more than one invoice lines
then all that lines not comes under only particular customer only at a time.
For example my output is coming like this:
Customer address: 3345 LIMITED-STUDIOS : 00033-45 PARR STREETLIVERPOOLCHESHIRE
Invoice Lines: 0001000402106-JUL-07INV 10.47
Customer address: 3345 LIMITED-STUDIOS : 00033-45 PARR STREETLIVERPOOLCHESHIRE
Invoice Lines: 0001000402713-JUL-07INV 10.77
But I am trying to get output like this:
Customer address: 3345 LIMITED-STUDIOS : 00033-45 PARR STREETLIVERPOOLCHESHIRE
Invoice Lines: 0001000402106-JUL-07INV 10.47
0001000402713-JUL-07INV 10.77
I am using fallowing code in my formula column:
function CF_UTL_FILEFormula return Char is
v_file_data utl_file.file_type;
begin
utl_file.put_line(v_file_data,:SEND_CUSTOMER_NAME:SEND_ADDRESS1:SEND_ADDRESS2:SEND_ADDRESS3:SEND_ADDRESS4:SEND_CITY:SEND_STATE:SEND_COUNTRY_DESC:SEND_POSTAL_CODE);
utl_file.put_line(v_file_data,LN1:CF_LOCATION:C_DELIVERY_DATE:INVOICE_NUMBER:TRX_DATE:c_transaction_type:CD_TRX_AMOUNT);
utl_file.fclose(v_file_data);
end;
Please help me how can I do this?What's the source of your Summary Column? It's not allowed to choose here the formula column in whcih you reference the summary column back. But another column should work.
As alternativ add a formula column in the upper group with
utl_file.put_line(v_file_data,:SEND_CUSTOMER_NAME:SEND_ADDRESS1:SEND_ADDRESS2:SEND_ADDRESS3:SEND_ADDRESS4:SEND_CITY:SEND_STATE:SEND_COUNTRY_DESC:SEND_POSTAL_CODE);
and remove the same line out of the other formula column. -
Little help needed on utl_file
Hi
I am trying to write a small stored procedure for recording some information to a text file, and I need a little help. However, beforehand, let me give you what I have done:
procedure create_record (order_id IN VARCHAR2) IS
l_dir VARCHAR2(10) 'c:/orders';
l_filename VARCHAR2(25) := 'orderlist';
l_datetime DATE := sysdate;
l_output utl_file.file_type;
begin
l_output := utl_file.fopen(l_dir, l_filename, 'w');
if !utl_file.fopen(l_output) THEN
utl_file.put_line(l_output, l_datetime || order_id);
utl_file.fclose(l_output);
else
utl_file.fclose(l_output);
l_output := utl_file.fopen(l_dir, l_filename, 'a');
end if;
end;
Now for questions, firstly, I am not entirely sure that the if statement for checking to see if an existing text file exists is the correct way, and would welcome some help on this, and correction.
Secondly, I need to add a second statement to the if, which if the file exists and the date on that file is the same as the system date, then to open and append some information to it, else create a new file.
Unfortunately, for me, I am not sure how to do this.
Can anyone show me how this can be done?
ThanksHope you can read my coding correctly.
ps: not tested
DECLARE
myFileExist BOOLEAN;
myFileLength NUMBER;
myFileBlockSize BINARY_INTEGER;
myText VARCHAR2(1000):= NULL;
myInstr NUMBER;
l_dir VARCHAR2(10) 'c:/orders';
l_filename VARCHAR2(25) := 'orderlist';
l_datetime DATE := trunc(sysdate);
l_output utl_file.file_type;
BEGIN
utl_file.fgetattr(l_dir, l_filename, myFileExist, myFileLength, myFileBlockSize);
if not myFileExist then -- file not exist
<ul>l_output := utl_file.fopen(l_dir, l_filename, 'w');
utl_file.put_line(l_output, l_datetime || order_id);</ul>
else --file exist
<ul>
l_output := utl_file.fopen(l_dir, l_filename, 'r');
LOOP
<ul>
--------------------------------------------------------------------start loop
UTL_FILE.GET_LINE(l_output,myText) ;
SELECT instr(UPPER(myText),UPPER(trunc(SYSDATE))) into myInstr FROM dual;
IF myText IS NULL THEN
<ul>
EXIT;
</ul>
END IF;
if myInstr > 0 then
<ul>
utl_file.fclose(l_output);
l_output := utl_file.fopen(l_dir, l_filename, 'a');
</ul>
else
<ul>
utl_file.fclose(l_output);
l_filename:='newfilename';
l_output := utl_file.fopen(l_dir, l_filename, 'w');
</ul>
end if;
--------------------------------------------------------------------end loop
</ul>
END loop;
utl_file.put_line(l_output, l_datetime || order_id);
</ul>
end if;
utl_file.fclose(l_output);
END;
Edited by: Lie Ching Te on 12-Feb-2010 12:34 PM
Edited by: Lie Ching Te on 12-Feb-2010 1:01 PM -
Help with utl_file (read/write file from local directory)
Need help reading/writing file on local machine from plsql using 10.2 DB.
I am trying to read/write a file from a local directory(laptop) without success.
I have been able to read/write to the database server directory but can't write to directory on local machine.
The utl_file_dir parm has been set to * and the db restarted but I can't get it to work... Here's the plsql statement.
out_file := UTL_FILE.FOPEN ( 'C:\PLSQL', 'TEST.TXT', 'W' ,32767);
Whenever I run it continues to write to c:\PLSQL dir on the database server. Have looked at the "Directory" object and created MY_DIR = C:\PLSQL but it writes to the db server.
Running 10.2 on a remote windows server, running PLSQL using sql*navigator.
Thanks in advance for your help..I don't see how you expect the server to be able to see your laptop across the network, hack into it and start writing files. Even if it could, what if there is more than one laptop with a C: drive? How would it know which one to write to?
Is there a shared drive on the server you can access via the laptop? -
Two message in different files widout help of UTL_FILE
Hi All..happy new year in advance..!
I am in a strange scenerio by my client.I am having below type of code in which I am writing log via
"dbms_output.put_line" but he needs that whenevr any error came into code eror message should be written in separate file but due to some ftp permission we cant use UTL_FILE option.
kind ly give me any idea,i hav done much r&d..help..!
spool msg_log.log
declare
raise_pc1 exception;
raise_pc2 exception;
begin
dbms_output.put_line('hi..this msg is from code block.');
raise raise_pc1;
exception
when raise_pc1 then
dbms_output.put_line('hi..this msg is from exception block.');
end;
spool off
exit;
o/p is- hi..this msg is from code block.
hi..this msg is from exception block.
above o/p is coming in same log file.
I need that 1st line should be in 1 file(ex-msg_log.log) &
2nd eror line should be in 2nd log file (ex-error_log.log)
rgds,
pcHi AP,
In this case everytime, when I will get exception in any step only 1 message will print bcoz my_stat = 1 for every exception & in 2nd block we have same 1 line to print.Kindly look ionto it also ..
var my_stat NUMBER;
SET SERVEROUTPUT ON
spool msg_log.log;
declare
raise_pc1 exception;
raise_pc2 exception;
begin
dbms_output.put_line('hi..this msg is from code block.');
begin
dbms_output.put_line('hi..raised raise_pc1.');
raise raise_pc1;
exception
when raise_pc1 then
:my_stat := 1;
end;
begin
dbms_output.put_line('hi..raised raise_pc2.');
raise raise_pc2;
exception
when raise_pc2 then
:my_stat := 1;
end;
end;
spool off;
spool err_log.log;
begin
if (:my_stat = 1) THEN
dbms_output.put_line('hi..this msg is from exception block.'||sqlerrm);
end if;
end;
spool off;
exit;
& o/p is --
hi..this msg is from exception block.ORA-0000: normal, successful completion
PL/SQL procedure successfully completed.
here I am getting only 1 line in o/p even 2 exception had raised..
rgds,
pc -
Help for a beginner in utl_file
Hi,
As I said I have just begun to learn pl/sql and looks like I am having a big trouble with it. I will attach the code I started with I know it does not do much and useful but can someone please help me run it? When I try to run it it gives the compilation error invalid directory path...
CREATE OR REPLACE PROCEDURE encrypt_to_file IS
/* Output variables to hold the result of the query: */
a ALL_OBJECTS.OWNER%TYPE;
b ALL_OBJECTS.OBJECT_NAME%TYPE;
vlocation VARCHAR2(16) := 'OUTPUT';
vopen_mode VARCHAR2(16) := 'w';
F1 UTL_FILE.FILE_TYPE;
/* Cursor declaration: */
CURSOR TCursor IS
SELECT OWNER, OBJECT_NAME
FROM ALL_OBJECTS;
BEGIN
OPEN TCursor;
F1 := utl_file.fopen( vlocation, 'DENEME.TXT', vopen_mode );
LOOP
/* Retrieve each row of the result of the above query
into PL/SQL variables: */
FETCH TCursor INTO a, b;
/* If there are no more rows to fetch, exit the loop: */
EXIT WHEN TCursor%NOTFOUND;
END LOOP;
UTL_FILE.FCLOSE(F1);
/* Free cursor used by the query. */
CLOSE TCursor;
END encrypt_to_file;
/OK, so someone has created a directory object for you.
What do you mean by "I don t get the error with its last condition as procedure"? Are you saying that you no longer get the error when running your procedure?
Are you looking in C:\Temp on the machine where Oracle is running for the file?
In the code you posted, you are not writing anything to the file. Can you add a UTL_FILE.PUT (or PUT_LINE) to ensure that there is something in the file?
Justin -
UTL_FILE.PUTF ...please help
Dear all:
I am trying to output the table result by using UTL_FILE.PUTF
function. I would like to seperate each column output by "tab",
however, I don't know what is the special function for outputing
the "tab". Anyone knows?
The sample code I have is as follows:
PROCEDURE TEST AS
v_outputfile UTL_FILE.file_type;
v1 RCP.POINT_IN_TIME.OWNER_URL_TYPE%TYPE;
v2 RCP.POINT_IN_TIME.DM_NAME%TYPE;
v3 RCP.POINT_IN_TIME.OWNER_URL_NAME%TYPE;
v4 RCP.POINT_IN_TIME.ID%TYPE;
CURSOR c_temp IS
SELECT * FROM CUSTOMER_RCP;
BEGIN
v_outputfile := UTL_FILE.FOPEN('/export','TEST.txt', 'w');
OPEN c_temp;
LOOP
FETCH c_temp INTO v1, v2, v3, v4;
EXIT WHEN c_temp%NOTFOUND;
UTL_FILE.PUTF(v_outputfile,'%s|%s|',v1,v2);
UTL_FILE.PUTF(v_outputfile,'%s|%s|',v3,v4);
UTL_FILE.PUTF(v_outputfile,'%s|%s\n',v1,v2);
END LOOP;
CLOSE c_temp;
UTL_FILE.FCLOSE(v_outputfile);
END TEST;
the "|" after %s is the seperator I want. However, I need
seperate them by "tab". What is the code for tab? I have
tried "\t" or "/t", but the seperator will return "\t" for the
seperator, not "tab"?
Please help...Hi there,
U can concatinate Chr(9) with column for which u want tab
spacing.
select Col1||chr(9),Col2...... from ....
Naveen -
Hi,
In utl file i have a reqruriment as " if file doesnt exit or unable to open the file for write "
For that i have coded below for checking a file is exit or not,
how to do the coding for " a file is been unable to open the file for write".
please advice.
BEGIN
var_out_file_name_1 BFILE := BFILENAME('path', 'x.rpt');
IF DBMS_LOB.FILEEXISTS (var_out_file_name_1) = 1 THEN
DBMS_OUTPUT.PUT_LINE ('File exists.');
ELSIF DBMS_LOB.FILEEXISTS (var_out_file_name_1) = 0 THEN
DBMS_OUTPUT.PUT_LINE ('File does not exist');
ELSE
DBMS_OUTPUT.PUT_LINE ('Unable to test existence for x.rpt');
END IF;
ENDoracle version : 8i
thanks in advance :-) Edited by: USER_X on Sep 10, 2008 3:26 PMmaybe exceptions might help if you will use the <a href="http://www.psoug.org/reference/utl_file.html" target="_blank">UTL_FILE</a> function.
BEGIN
EXCEPTION
WHEN utl_file.invalid_mode THEN
dbms_output.put_line ('Invalid Mode Parameter');
WHEN utl_file.invalid_path THEN
dbms_output.put_line ('Invalid File Location');
WHEN utl_file.invalid_filehandle THEN
dbms_output.put_line ('Invalid Filehandle');
WHEN utl_file.invalid_operation THEN
dbms_output.put_line ('Invalid Operation');
WHEN utl_file.read_error THEN
dbms_output.put_line ('Read Error');
WHEN utl_file.internal_error THEN
dbms_output.put_line ('Internal Error');
WHEN utl_file.charsetmismatch THEN
dbms_output.put_line ('Opened With FOPEN_NCHAR But Later I/O Inconsistent');
WHEN utl_file.file_open THEN
dbms_output.put_line ('File Already Opened');
WHEN utl_file.invalid_maxlinesize THEN
dbms_output.put_line('Line Size Exceeds 32K');
WHEN utl_file.invalid_filename THEN
dbms_output.put_line ('Invalid File Name');
WHEN utl_file.access_denied THEN
dbms_output.put_line ('File Access Denied By');
WHEN utl_file.invalid_offset THEN
dbms_output.put_line ('FSEEK Param Less Than 0');
WHEN others THEN
dbms_output.put_line ('Unknown UTL_FILE Error');
END; -
Help on UTL_FILE.INVALID_FILEHANDLE, please
Hi to all of you,
My name is Marcello Cremente and I need some help about a "strange case" that happens to me.
I wrote a package that heavily uses the UTL_FILE package in order to log actions my own package does.
I successfully created files of 200MB and more. Since one week I've got the exception UTL_FILE.INVALID_FILEHANDLE when issuing a UTL_FILE.PUT_LINE. Of course the file contains already thousands of line when this happens.
Any hint is very appreciated!
Thanks in advance
Marcello Cremente ([email protected])Thanks for the response.
yes, there's plenty of space on disk.
No, actually I don't issue any UTL_FILE.FFLUSH, but it don't seem to be related with the exception I've got. After having used repeatedly the file_handle, it become suddenly invalid! WHY? -
Utl_file issue..plzz help
Hi alll...
Sorry, If this question is already answered in some other posts.
Database: 11g
I am planning to generate a CSV by selecting some data from the database.
I am using UTL_file for this.But the main issue is, if ""each line size"" is less than 32k.
Everything is working fine.
The problem is some of my ""lines are more than 32k"".
each line. I am putting it in a CLOB. (as the line can more that 32k).
It seems utl_file.put_line has a limitation of 32k.Tried the following code:
Code 1:
v_clobLen := DBMS_LOB.GETLENGTH(out_str);
dbms_output.put_line('clob length '||v_clobLen);
WHILE v_pos < v_clobLen
LOOP
v_buffer := DBMS_LOB.SUBSTR(out_str, v_amount, v_pos);
EXIT
WHEN v_buffer IS NULL;
v_chr10 := INSTR(v_buffer,CHR(10),-1);
IF v_chr10 != 0 THEN
v_buffer := SUBSTR(v_buffer,1,v_chr10-1);
END IF;
UTL_FILE.PUT_LINE(fileHandler, v_buffer,TRUE);
v_pos := v_pos + LEAST(LENGTH(v_buffer)+1,v_amount);
END LOOP;The problem with code 1 is
It seems that utl_file.put_line puts a carriage return after every 32k.because of that, I am getting the output,
but the carriage returns are messing up my CSV fileANOTHER CODE 2:
FOR i IN 0 .. TRUNC ( (DBMS_LOB.getlength (out_str) - 1) / t_len)
LOOP
UTL_FILE.put_raw (fileHandler, DBMS_LOB.SUBSTR (out_str, t_len, i * t_len + 1));
END LOOP;Intead of utl_file.put_file, i tried utl_file.put_raw.
But somehow some of the data is getting truncating.
Not sure what is the issue with the Code 2:
Please help me with the code that can handle more that 32k data and can write to a CSV file.
Thanksrp0428 wrote:
You can use PUT and then add your own line terminators. Use NEW_LINE and it will add platform specific terminators.No, you can not. UTL_FILE is a package to read and write operating system *<font color=red>text</font>* files. And UTL_FILE restriction is: file is a stream file with NEW_LINE serving as record (line) separator. Max record size is whatever specified in FOPEN as parameter max_linesize and can not exceed 32767 (including new line). Below is an example:
SQL> DECLARE
2 f UTL_FILE.FILE_TYPE := UTL_FILE.FOPEN('TEMP', 'FileName.txt', 'w', 32767);
3 buffer varchar2(10000) := LPAD('A',9999,'B') || 'A';
4 BEGIN
5 UTL_FILE.PUT(f, buffer);
6 UTL_FILE.PUT(f, buffer);
7 UTL_FILE.PUT(f, buffer);
8 UTL_FILE.FCLOSE(f);
9 END;
10 /
PL/SQL procedure successfully completed.
SQL> As you can see, when total size does not exceed 32K, it works fine, but as soon as it exceeds 32K:
SQL> DECLARE
2 f UTL_FILE.FILE_TYPE := UTL_FILE.FOPEN('TEMP', 'FileName.txt', 'w', 32767);
3 buffer varchar2(10000) := LPAD('A',9999,'B') || 'A';
4 BEGIN
5 UTL_FILE.PUT(f, buffer);
6 UTL_FILE.PUT(f, buffer);
7 UTL_FILE.PUT(f, buffer);
8 UTL_FILE.PUT(f, buffer); -- here we are exceeding 32767
9 UTL_FILE.FCLOSE(f);
10 END;
11 /
DECLARE
ERROR at line 1:
ORA-29285: file write error
ORA-06512: at "SYS.UTL_FILE", line 77
ORA-06512: at "SYS.UTL_FILE", line 690
ORA-06512: at line 9
SQL> What you can is open file in binary mode, then:
SQL> DECLARE
2 f UTL_FILE.FILE_TYPE := UTL_FILE.FOPEN('TEMP', 'FileName.txt', 'wb', 32767);
3 buffer varchar2(10000) := LPAD('A',9999,'B') || 'A';
4 BEGIN
5 UTL_FILE.PUT_RAW(f, UTL_RAW.CAST_TO_RAW(buffer));
6 UTL_FILE.PUT_RAW(f, UTL_RAW.CAST_TO_RAW(buffer));
7 UTL_FILE.PUT_RAW(f, UTL_RAW.CAST_TO_RAW(buffer));
8 UTL_FILE.PUT_RAW(f, UTL_RAW.CAST_TO_RAW(buffer)); -- here we are exceeding 32767
9 UTL_FILE.FCLOSE(f);
10 END;
11 /
PL/SQL procedure successfully completed.
SQL>SY. -
Need Help: UTL_FILE Reading and Writing to Text File
Hello I am using version 11gR2 using the UTL_FILE function to read from a text file then write the lines where it begins with word 'foo' and end my writing to the text file where the line with the word 'ZEN' is found. Now, I have several lines that begin with 'foo' and 'ZEN' Which make for one full paragraph, and in this paragraph there's a line that begins with 'DE4.2'. Therefore,
I need to write all paragraphs that include the line 'DE4.2' in their beginning and ending lines 'foo' and 'ZEN'
FOR EXAMPLE:
FOO/234E53LLID
THIS IS MY SECOND LINE
THIS IS MY THIRD LINE
DE4.2 THIS IS MY FOURTH LINE
THIS IS MY FIFTH LINE
ZEN/DING3434343
FOO/234E53LLID
THIS IS MY SECOND LINE
THIS IS MY THIRD LINE
THIS IS MY FIFTH LINE
ZEN/DING3434343
I am only interested in writing the first paragraph tha includes line DE4.2 in one of ther lines Not the Second paragraph that does not include the 'DE4.2'
Here's my code thus far:
CREATE OR REPLACE PROCEDURE my_app2 IS
infile utl_file.file_type;
outfile utl_file.file_type;
buffer VARCHAR2(30000);
b_paragraph_started BOOLEAN := FALSE; -- flag to indicate that required paragraph is started
BEGIN
-- open a file to read
infile := utl_file.fopen('TEST_DIR', 'mytst.txt', 'r');
-- open a file to write
outfile := utl_file.fopen('TEST_DIR', 'out.txt', 'w');
-- check file is opened
IF utl_file.is_open(infile)
THEN
-- loop lines in the file
LOOP
BEGIN
utl_file.get_line(infile, buffer);
--BEGINPOINT APPLICATION
IF buffer LIKE 'foo%' THEN
b_paragraph_started := TRUE;
END IF;
--LOOK FOR GRADS APPS
IF b_paragraph_started AND buffer LIKE '%DE4%' THEN
utl_file.put_line(outfile,buffer, FALSE);
END IF;
--ENDPOINT APPLICATION
IF buffer LIKE 'ZEN%' THEN
b_paragraph_started := FALSE;
END IF;
utl_file.fflush(outfile);
EXCEPTION
WHEN no_data_found THEN
EXIT;
END;
END LOOP;
END IF;
utl_file.fclose(infile);
utl_file.fclose(outfile);
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20099, 'Unknown UTL_FILE Error');
END my_app2;
When I run this code I only get one line: DE4.2 I AM MISSING THE ENTIRE PARAGRAPH
PLEASE ADVISE...Hi,
Look at where you're calling utl_file.put_line. The only time you're writing anything is immediately after you find the the key word 'DE4', and then you're writing just that line.
You need to store the entire paragraph, and when you reach the end of the paragraph, write the whole thing only if you found the key word, like this:
CREATE OR REPLACE PROCEDURE my_app2 IS
TYPE line_collection
IS TABLE OF VARCHAR2 (30000)
INDEX BY BINARY_INTEGER;
infile utl_file.file_type;
outfile utl_file.file_type;
input_paragraph line_collection;
input_paragraph_cnt PLS_INTEGER := 0; -- Number of lines stored in input_paragraph
b_paragraph_started BOOLEAN := FALSE; -- flag to indicate that required paragraph is started
found_key_word BOOLEAN := FALSE; -- Does this paragraph contain the magic word?
BEGIN
-- open a file to read
infile := utl_file.fopen('TEST_DIR', 'mytst.txt', 'r');
-- open a file to write
outfile := utl_file.fopen('TEST_DIR', 'out.txt', 'w');
-- check file is opened
IF utl_file.is_open(infile)
THEN
-- loop lines in the file
LOOP
BEGIN
input_paragraph_cnt := input_paragraph_cnt + 1;
utl_file.get_line (infile, input_paragraph (input_paragraph_cnt));
--BEGINPOINT APPLICATION
IF LOWER (input_paragraph (input_paragraph_cnt)) LIKE 'foo%' THEN
b_paragraph_started := TRUE;
END IF;
--LOOK FOR GRADS APPS
IF b_paragraph_started
THEN
IF input_paragraph (input_paragraph_cnt) LIKE '%DE4%'
THEN
found_key_word := TRUE;
END IF;
--ENDPOINT APPLICATION
IF input_paragraph (input_paragraph_cnt) LIKE 'ZEN%' THEN
b_paragraph_started := FALSE;
IF found_key_word
THEN
FOR j IN 1 .. input_paragraph_cnt
LOOP
utl_file.put_line (outfile, input_paragraph (j), FALSE);
END LOOP;
END IF;
found_key_word := FALSE;
input_paragraph_cnt := 0;
END IF;
ELSE -- paragraph is not started
input_paragraph_cnt := 0;
END IF;
EXCEPTION
WHEN no_data_found THEN
EXIT;
END;
END LOOP;
END IF;
utl_file.fclose (infile);
utl_file.fclose (outfile);
--EXCEPTION
-- WHEN OTHERS THEN
-- raise_application_error(-20099, 'Unknown UTL_FILE Error');
END my_app2;
SHOW ERRORSIf you don't have an EXCEPTION section, the default error handling will print an error message, spcifying exactly what the error was, and which line of your code caused the error. By using your own EXCEPTION section, you're hiding all that information. I admit, the error messages aren't always as informative as we'd like, but they're never less informative than "Unknown UTL_FILE Error'. Don't use your own EXCEPTION handling unless you can improve on the default.
Remember that anything inside quotes is case-sensitive. If your file contains upper-case 'FOO', then it won't be "LIKE 'foo%' ".
Edited by: Frank Kulash on Dec 7, 2011 1:35 PM
You may have noticed that this site normally doesn't display multiple spaces in a row.
Whenever you post formatted text (such as your code) on this site, type these 6 characters:
\{code}
(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.
Maybe you are looking for
-
When I load the photos from the file or files i want from my computer, it will not display the file or files on the iTunes work area for my iPod. so i can arange my photos, but it dose load it in to my iPod. When I sync the file it says "up dateing i
-
External Lacie drive won't mount
I have a older Lacie 160GB LaCie P3 extrenal drive that I can't mount. There never has been an issue with mounting it before in the method described below. Here is what happened: I turned the drive on but the firewire was not connected so I turned th
-
JAVA arithmetic problem with double ?
If I try to work double precision numbers I get a unintelligible result, but what do I do wrong ? Are there any inaccuracy in the JAVA double precision numbers arithmetic or what's the problem with this very simple example code ? How can I get correc
-
Change credit control area in posted document
Hi, how can I make this field KKBER changeable in an already posted document? I have made settings in OB38 that it can be overwritten during document entry and that is working fine. Additonally, I have added the field BSEG-KKBER in document change ru
-
Switch to Optimum Online?
I have been unable to use DSL for more than a week now. So, I am wondering if it is easy to switch to Optimum Online. Does anyone have tried Optimum Online? Was it easy to install? Compared to Verizon, how is the Optimum Online? Thank you so muc