Home » SQL & PL/SQL » SQL & PL/SQL » Loop in UTL_FILE is not getting closed (oracle 11.2.0.3.0 - Windows server 2008 R2)
Loop in UTL_FILE is not getting closed [message #600122] Fri, 01 November 2013 01:45 Go to next message
muktha_22
Messages: 527
Registered: December 2009
Senior Member
Hi All,

I am trying read a File to find out the error message through UTL_FILE.
But the loop used inside is not getting closed.
Kindly help me on this.

CREATE OR REPLACE procedure alert_mail_A
as
v_flag varchar2(10);
mesg varchar2(100);
c1 utl_smtp.connection; -- passing the datatype to a variable.
c varchar2 (1000);
B utl_file.file_type;
AB utl_file.file_type;
A varchar2(1000) := 'anatb_logA_29-OCT-2013log.LST';
v_checkA varchar2(1000);
v_check PLS_INTEGER;
v_output varchar2(2500);
v_output_msg varchar2(2500);
v_subject varchar2(250):= 'The ANATB is Ran Successfully by DC Support Team';
v_error_msg varchar2(2500);

begin
    B :=utl_file.fopen('ANATBLOG',A,'R',32000);
   loop
   begin
   utl_file.get_line(B,c,32000);
   EXCEPTION
         WHEN NO_DATA_FOUND THEN
            null;
         WHEN value_error THEN
            utl_file.fclose_all;
            RAISE_APPLICATION_ERROR
            ( -20001, 'line too long to store in buffer');
         WHEN utl_file.invalid_operation THEN
            utl_file.fclose_all;
            RAISE_APPLICATION_ERROR
            ( -20001, 'file is not open for reading');
         WHEN utl_file.read_error THEN
            utl_file.fclose_all;
            RAISE_APPLICATION_ERROR
            ( -20001, 'OS error occurred during read');
   end;
   
 begin
   --v_output :=   (substr(c,1,100));
   --dbms_output.put_line('The first line is' ||c);
 --IF INSTR(c, 'ORA-') >= 1
    if INSTR(c, 'ORA-') >= 1
        THEN
        v_output :=   (substr(c,1,100));
    dbms_output.put_line(v_output); 
     insert into anatb_alert
      values(v_output);
     commit;
    end if;
 exception
     when no_data_found then
     exit
 end;
 end loop;
   
    dbms_output.put_line('No more lines'); 
v_check:=INSTR(c,'SQL> spool off'); --this is case sensitive 
utl_file.fclose(B);
end;


Regards
Muktha
Re: Loop in UTL_FILE is not getting closed [message #600125 is a reply to message #600122] Fri, 01 November 2013 01:58 Go to previous message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
First of all, you posted ugly unformatted code which can not be formatted (using any SQL formatter) because it contains syntax error(s). It is difficult to spot what is going on in such an unformatted code.

As of your question: the only EXIT (if that's the place you thought it will exit the loop) is on the wrong place. That piece of code (IF statement) can't raise NO-DATA-FOUND. Unless I'm wrong, EXIT should have been placed in the first EXCEPTION handler section you wrote, within the NO-DATA-FOUND:
loop
  begin
    utl_file.get_line(B,c,32000);
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      null;                --> here!
Previous Topic: CREATE New Table from existing tables
Next Topic: Constraints in Describe
Goto Forum:
  


Current Time: Thu Apr 25 00:38:34 CDT 2024