Home » SQL & PL/SQL » SQL & PL/SQL » Dynamic File Creation using UTL_FILE (Oracle 11G)
Dynamic File Creation using UTL_FILE [message #649746] Tue, 05 April 2016 02:58 Go to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

Hello,
I have the following code which is creating 32 output files based on 32 different states. For each state I have hard coded the values in validation conditions but I want to know if I could create this dynamically. I have tried embedding the concatenated state with output file but its not working. I also tried declaring a varchar variable for output file name but UTL file still throws an error that I could not use char variable inside it. Please provide some pointers so I can modify accordingly.

Thanks!

DECLARE

  w_dir    VARCHAR2(100);
  w_input_file  VARCHAR2(50);
  w_AL_output_file VARCHAR2(50);
  w_AR_output_file VARCHAR2(50);
  w_AZ_output_file VARCHAR2(50);
  w_CO_output_file VARCHAR2(50);
  w_CT_output_file VARCHAR2(50);
  w_DE_output_file VARCHAR2(50);
  w_FL_output_file VARCHAR2(50);
  w_GA_output_file VARCHAR2(50);
  w_HI_output_file VARCHAR2(50);
  w_IA_output_file VARCHAR2(50);
  w_ID_output_file VARCHAR2(50);
  w_IL_output_file VARCHAR2(50);
  w_IN_output_file VARCHAR2(50);
  w_KS_output_file VARCHAR2(50);
  w_KY_output_file VARCHAR2(50);
  w_MD_output_file VARCHAR2(50);
  w_MI_output_file VARCHAR2(50);
  w_MN_output_file VARCHAR2(50);
  w_MO_output_file VARCHAR2(50);
  w_MS_output_file VARCHAR2(50);
  w_MT_output_file VARCHAR2(50);
  w_NC_output_file VARCHAR2(50);
  w_NE_output_file VARCHAR2(50);
  w_NM_output_file VARCHAR2(50);
  w_NV_output_file VARCHAR2(50);
  w_OK_output_file VARCHAR2(50);
  w_OR_output_file VARCHAR2(50);
  w_SC_output_file VARCHAR2(50);
  w_TN_output_file VARCHAR2(50);
  w_VA_output_file VARCHAR2(50);
  w_WI_output_file VARCHAR2(50);
  w_WV_output_file VARCHAR2(50);
  w_newline	   VARCHAR2(2000);
  w_val            VARCHAR2(200);
  w_polnum         VARCHAR2(20);
  w_polid          NUMBER;
  w_state          CHAR(2);
  w_wcrtg	   ops$argdba.arg_pr_wc_rtg_basis%ROWTYPE;

  input_file	  utl_file.file_type;
  AL_output_file  utl_file.file_type;
  AR_output_file  utl_file.file_type;
  AZ_output_file  utl_file.file_type;
  CO_output_file  utl_file.file_type;
  CT_output_file  utl_file.file_type;
  DE_output_file  utl_file.file_type;
  FL_output_file  utl_file.file_type;
  GA_output_file  utl_file.file_type;
  HI_output_file  utl_file.file_type;
  IA_output_file  utl_file.file_type;
  ID_output_file  utl_file.file_type;
  IL_output_file  utl_file.file_type;
  IN_output_file  utl_file.file_type;
  KS_output_file  utl_file.file_type;
  KY_output_file  utl_file.file_type;
  MD_output_file  utl_file.file_type;
  MI_output_file  utl_file.file_type;
  MN_output_file  utl_file.file_type;
  MO_output_file  utl_file.file_type;
  MS_output_file  utl_file.file_type;
  MT_output_file  utl_file.file_type;
  NC_output_file  utl_file.file_type;
  NE_output_file  utl_file.file_type;
  NM_output_file  utl_file.file_type;
  NV_output_file  utl_file.file_type;
  OK_output_file  utl_file.file_type;
  OR_output_file  utl_file.file_type;
  SC_output_file  utl_file.file_type;
  TN_output_file  utl_file.file_type;
  VA_output_file  utl_file.file_type;
  WI_output_file  utl_file.file_type;
  WV_output_file  utl_file.file_type;  
  
  cursor c1(w_polnum varchar2) is
  select pol_id
    from ops$argdba.arg_pr_pol
   where pol_pfx_cd || div || myc || pol_ser_num || ml = w_polnum;
         
  cursor c2(w_polid number) is
        select distinct st_cd_alpha
          from ops$argdba.arg_pr_pol_st
         where pol_id = w_polid;
         
  cursor c3(w_polid number, w_state varchar2) is 
        select *
           from ops$argdba.arg_pr_wc_rtg_basis
          where pol_id = w_polid
            and st_cd_alpha = w_state;
  


BEGIN
   
   dbms_output.enable(2000000);

    w_dir            := 'TEST2'; --AIS Directory
    --w_dir            := '/cent/dev0/batch/output'; --GCIS Directory
    w_input_file     := 'wcpols_test.txt';   --Original WCPOLS file;
    w_AL_output_file := 'wcpolstest.AL';
    w_AR_output_file := 'wcpolstest.AR';
    w_AZ_output_file := 'wcpolstest.AZ';
    w_CO_output_file := 'wcpolstest.CO';
    w_CT_output_file := 'wcpolstest.CT';
    w_DE_output_file := 'wcpolstest.DE';
    w_FL_output_file := 'wcpolstest.FL';
    w_GA_output_file := 'wcpolstest.GA';
    w_HI_output_file := 'wcpolstest.HI';
    w_IA_output_file := 'wcpolstest.IA';
    w_ID_output_file := 'wcpolstest.ID';
    w_IL_output_file := 'wcpolstest.IL';
    w_IN_output_file := 'wcpolstest.IN';
    w_KS_output_file := 'wcpolstest.KS';
    w_KY_output_file := 'wcpolstest.KY';
    w_MD_output_file := 'wcpolstest.MD';
    w_MI_output_file := 'wcpolstest.MI';
    w_MN_output_file := 'wcpolstest.MN';
    w_MO_output_file := 'wcpolstest.MO';
    w_MS_output_file := 'wcpolstest.MS';
    w_MT_output_file := 'wcpolstest.MT';
    w_NC_output_file := 'wcpolstest.NC';
    w_NE_output_file := 'wcpolstest.NE';
    w_NM_output_file := 'wcpolstest.NM';
    w_NV_output_file := 'wcpolstest.NV';
    w_OK_output_file := 'wcpolstest.OK';
    w_OR_output_file := 'wcpolstest.OR';
    w_SC_output_file := 'wcpolstest.SC';
    w_TN_output_file := 'wcpolstest.TN';
    w_VA_output_file := 'wcpolstest.VA';
    w_WI_output_file := 'wcpolstest.WI';
    w_WV_output_file := 'wcpolstest.WV';

    
    input_file    := utl_file.fopen(w_dir, w_input_file, 'r',32000);
    AL_output_file   := utl_file.fopen(w_dir, w_AL_output_file, 'w',32000);
    AR_output_file   := utl_file.fopen(w_dir, w_AR_output_file, 'w',32000);
    AZ_output_file   := utl_file.fopen(w_dir, w_AZ_output_file, 'w',32000);
    CO_output_file   := utl_file.fopen(w_dir, w_CO_output_file, 'w',32000);
    CT_output_file   := utl_file.fopen(w_dir, w_CT_output_file, 'w',32000);
    DE_output_file   := utl_file.fopen(w_dir, w_DE_output_file, 'w',32000);
    FL_output_file   := utl_file.fopen(w_dir, w_FL_output_file, 'w',32000);
    GA_output_file   := utl_file.fopen(w_dir, w_GA_output_file, 'w',32000);
    HI_output_file   := utl_file.fopen(w_dir, w_HI_output_file, 'w',32000);
    IA_output_file   := utl_file.fopen(w_dir, w_IA_output_file, 'w',32000);
    ID_output_file   := utl_file.fopen(w_dir, w_ID_output_file, 'w',32000);
    IL_output_file   := utl_file.fopen(w_dir, w_IL_output_file, 'w',32000);
    IN_output_file   := utl_file.fopen(w_dir, w_IN_output_file, 'w',32000);
    KS_output_file   := utl_file.fopen(w_dir, w_KS_output_file, 'w',32000);
    KY_output_file   := utl_file.fopen(w_dir, w_KY_output_file, 'w',32000);
    MD_output_file   := utl_file.fopen(w_dir, w_MD_output_file, 'w',32000);
    MI_output_file   := utl_file.fopen(w_dir, w_MI_output_file, 'w',32000);
    MN_output_file   := utl_file.fopen(w_dir, w_MN_output_file, 'w',32000);
    MO_output_file   := utl_file.fopen(w_dir, w_MO_output_file, 'w',32000);
    MS_output_file   := utl_file.fopen(w_dir, w_MS_output_file, 'w',32000);
    MT_output_file   := utl_file.fopen(w_dir, w_MT_output_file, 'w',32000);
    NC_output_file   := utl_file.fopen(w_dir, w_NC_output_file, 'w',32000);
    NE_output_file   := utl_file.fopen(w_dir, w_NE_output_file, 'w',32000);
    NM_output_file   := utl_file.fopen(w_dir, w_NM_output_file, 'w',32000);
    NV_output_file   := utl_file.fopen(w_dir, w_NV_output_file, 'w',32000);
    OK_output_file   := utl_file.fopen(w_dir, w_OK_output_file, 'w',32000);
    OR_output_file   := utl_file.fopen(w_dir, w_OR_output_file, 'w',32000);
    SC_output_file   := utl_file.fopen(w_dir, w_SC_output_file, 'w',32000);
    TN_output_file   := utl_file.fopen(w_dir, w_TN_output_file, 'w',32000);
    VA_output_file   := utl_file.fopen(w_dir, w_VA_output_file, 'w',32000);
    WI_output_file   := utl_file.fopen(w_dir, w_WI_output_file, 'w',32000);
    WV_output_file   := utl_file.fopen(w_dir, w_WV_output_file, 'w',32000);

    IF utl_file.is_open(input_file) THEN
     LOOP
       BEGIN

         utl_file.get_line(input_file, w_newline);
         
         w_polnum := substr(w_newline,6,14);
         for i in c1(w_polnum) loop 
           for j in c2(i.pol_id) loop
             for k in c3(i.pol_id, j.st_cd_alpha) loop
               w_val := k.st_cd_alpha||' '||k.prem_amt;
               
               if k.st_cd_alpha = 'AL' then   
                   utl_file.put_line(AL_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'AR' then   
                   utl_file.put_line(AR_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'AZ' then   
                   utl_file.put_line(AZ_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'CO' then   
                   utl_file.put_line(CO_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'CT' then   
                   utl_file.put_line(CT_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'DE' then   
                   utl_file.put_line(DE_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'FL' then   
                   utl_file.put_line(FL_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'GA' then   
                   utl_file.put_line(GA_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'HI' then   
                   utl_file.put_line(HI_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'IA' then   
                   utl_file.put_line(IA_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'ID' then   
                   utl_file.put_line(ID_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'IL' then   
                   utl_file.put_line(IL_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'IN' then   
                   utl_file.put_line(IN_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'KS' then   
                   utl_file.put_line(KS_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'KY' then   
                   utl_file.put_line(KY_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'MD' then   
                   utl_file.put_line(MD_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'MI' then   
                   utl_file.put_line(MI_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'MN' then   
                   utl_file.put_line(MN_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'MO' then   
                   utl_file.put_line(MO_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'MS' then   
                   utl_file.put_line(MS_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'MT' then   
                   utl_file.put_line(MT_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'NC' then   
                   utl_file.put_line(NC_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'NE' then   
                   utl_file.put_line(NE_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'NM' then   
                   utl_file.put_line(NM_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'NV' then   
                   utl_file.put_line(NV_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'OK' then   
                   utl_file.put_line(OK_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'OR' then   
                   utl_file.put_line(OR_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'SC' then   
                   utl_file.put_line(SC_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'TN' then   
                   utl_file.put_line(TN_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'VA' then   
                   utl_file.put_line(VA_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'WI' then   
                   utl_file.put_line(WI_output_file,w_val);
                end if;
                if k.st_cd_alpha = 'WV' then   
                   utl_file.put_line(WV_output_file,w_val);
                end if;

             end loop;
           end loop;
         end loop;            
                 

       EXCEPTION WHEN NO_DATA_FOUND THEN
           EXIT;
       END;
      END LOOP;

    END IF;  --is_open
   
   utl_file.fclose(input_file);
   utl_file.fclose(AL_output_file);
    utl_file.fclose(AR_output_file);
    utl_file.fclose(AZ_output_file);
    utl_file.fclose(CO_output_file);
    utl_file.fclose(CT_output_file);
    utl_file.fclose(DE_output_file);
    utl_file.fclose(FL_output_file);
    utl_file.fclose(GA_output_file);
    utl_file.fclose(HI_output_file);
    utl_file.fclose(IA_output_file);
    utl_file.fclose(ID_output_file);
    utl_file.fclose(IL_output_file);
    utl_file.fclose(IN_output_file);
    utl_file.fclose(KS_output_file);
    utl_file.fclose(KY_output_file);
    utl_file.fclose(MD_output_file);
    utl_file.fclose(MI_output_file);
    utl_file.fclose(MN_output_file);
    utl_file.fclose(MO_output_file);
    utl_file.fclose(MS_output_file);
    utl_file.fclose(MT_output_file);
    utl_file.fclose(NC_output_file);
    utl_file.fclose(NE_output_file);
    utl_file.fclose(NM_output_file);
    utl_file.fclose(NV_output_file);
    utl_file.fclose(OK_output_file);
    utl_file.fclose(OR_output_file);
    utl_file.fclose(SC_output_file);
    utl_file.fclose(TN_output_file);
    utl_file.fclose(VA_output_file);
    utl_file.fclose(WI_output_file);
    utl_file.fclose(WV_output_file);
   
   
END;




[Edit MC: add code tags]

[Updated on: Tue, 05 April 2016 03:28] by Moderator

Report message to a moderator

Re: Dynamic File Creation using UTL_FILE [message #649747 is a reply to message #649746] Tue, 05 April 2016 02:59 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

ops, the code didn't get formatted properly. Sorry for this.
Re: Dynamic File Creation using UTL_FILE [message #649748 is a reply to message #649747] Tue, 05 April 2016 03:17 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
That would be because you didn't use [code] tags
Read the link and follow the instructions
Re: Dynamic File Creation using UTL_FILE [message #649750 is a reply to message #649746] Tue, 05 April 2016 03:31 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I don't understand what is your problem but, given the code, I'd use an array of file type and an associative array of utl_file.file_type indexed by the file type.

Re: Dynamic File Creation using UTL_FILE [message #649751 is a reply to message #649750] Tue, 05 April 2016 04:17 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
I'd use an external table for the input file then query the data in st_cd_alpha order so you only have to have one output file open at a time. Just concatenate the appropriate extension into the file_name variable each time.
Re: Dynamic File Creation using UTL_FILE [message #649754 is a reply to message #649751] Tue, 05 April 2016 06:29 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

Thank you for your inputs. I will try to store state codes in pl/sql table and try to open file with that name. Ill update here my results.

Thank you!
Re: Dynamic File Creation using UTL_FILE [message #649760 is a reply to message #649754] Tue, 05 April 2016 08:22 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
That's over-complicating it. If you use my approach you can have something like this:
DECLARE

..........

BEGIN
  

  FOR rec IN (<select that queries external table and joins to three tables from the existing cursors, ordered by st_cd_alpha>) LOOP
    
    IF rec.st_cd_alpha != l_prev_alpha OR l_prev_alpha IS NULL THEN

      --got a new alpha code      

      IF l_prev_alpha IS NOT NULL THEN
        --close existing file
        utl_file.fclose(l_output_file);
        
      END IF;
      
      --define and open new file
      l_output_file_name := 'wcpolstest.'||rec.st_cd_alpha;
      l_output_file := utl_file.fopen(w_dir, l_output_file_name, 'w',32000);
      
    END IF;
    
    utl_file.put_line(l_output_file,rec.val);
    
    --record current alpha
    l_prev_alpha := rec.st_cd_alpha;

  END LOOP;

  --close last file
  utl_file.fclose(l_output_file);
  
END;


Create the external table, fill in the query and the variable declarations and you should be good to go.
Re: Dynamic File Creation using UTL_FILE [message #650804 is a reply to message #649760] Wed, 04 May 2016 02:34 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

Hello-
I am not sure where I am making mistake but the ouput_file in condition if "l_exists then" is not getting the correct file. The below code should write some lines in file 1, file 2, file 3 and again in file 1 but the last file 1 records are getting into file 3 because the current file is file 3. Depending on file_num from wcpols_submission_file table it should print the lines from input file into multiple files like PPEP_MI_1, _2 and _3.txt. Now the last records which should go to _1 file are getting into _3. Please guide me, Thanks

DECLARE

  w_dir                   VARCHAR2(100);
  w_input_file            VARCHAR2(50);
  w_newline               VARCHAR2(2000);
  w_NewPolNum             VARCHAR2(20);
  w_NewRecTyp                CHAR(2);
  w_NewTranDt             CHAR(5);
  w_NewTranCd             CHAR(2);  
  w_file_num             NUMBER;

  input_file    utl_file.file_type;
  output_file   utl_file.file_type;
  w_Pol_Header number := 0;
  
  l_exists boolean;
  l_size number;
  l_block_size number;
  
BEGIN
   dbms_output.enable(null);
    w_dir            := 'CENGROUP_OUTPUT'; 
    w_input_file     := 'wcpols_04152016_04222016_MI.txt';  

    input_file    := utl_file.fopen(w_dir, w_input_file, 'r',32000);
  
    IF utl_file.is_open(input_file) THEN
     LOOP
     BEGIN

         utl_file.get_line(input_file, w_newline);
         w_NewPolNum  := substr(w_newline,6,14);
         w_NewRecTyp  := substr(w_newline,46,2);
         w_NewTranDt  := substr(w_newline,37,5);
         w_TranTypCd  := substr(w_newline,42,2);
         
         
         if w_NewPolNum = substr(w_newline,6,14) and w_NewTranDt = substr(w_newline,37,5) and w_NewRecTyp in ('01','08') then
           w_pol_header := w_pol_header + 1;     
         end if;
         
         select file_num
           into w_file_num
           from wcpols_submission_files
          where pol_header = w_pol_header;
          
          utl_file.fgetattr( w_dir, 'PPEP_MI_'||w_file_num||'.txt', l_exists, l_size, l_block_size );

           IF NOT ( l_exists ) THEN
              output_file   := utl_file.fopen(w_dir, 'PPEP_MI_'||w_file_num||'.txt', 'w',32000);          
           END IF;
          
          if l_exists then
             utl_file.put_line(output_file,w_file_num||'-'||w_NewPolNum||'-'||w_NewTranDt||'-'||w_TranTypCd||'-'||w_NewRecTyp);
          end if;
          
               
      EXCEPTION WHEN NO_DATA_FOUND THEN
          EXIT;
       END;
       
      END LOOP; --main loop
    END IF;  --InputFile is_open

   utl_file.fclose(input_file);
   utl_file.fclose(output_file);

END; 


Re: Dynamic File Creation using UTL_FILE [message #650805 is a reply to message #650804] Wed, 04 May 2016 03:04 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
You're only setting output_file when the file doesn't exist. You need to set it every time file_num changes. So you need to store the previous value of file_num to compare it to the current value in each iteration of the loop. You'll also need to close each file before opening a new one - so you need a close in the loop.
Re: Dynamic File Creation using UTL_FILE [message #650806 is a reply to message #650805] Wed, 04 May 2016 03:11 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
In fact I wouldn't bother with fgetattr at all. Just open the files in append mode.
Re: Dynamic File Creation using UTL_FILE [message #650807 is a reply to message #650806] Wed, 04 May 2016 03:15 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Looking at it, you can move the select and the file open/close code inside the IF that increments w_pol_header. Since if w_pol_header doesn't change the file you're using can't.
Re: Dynamic File Creation using UTL_FILE [message #650808 is a reply to message #650807] Wed, 04 May 2016 03:17 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
The first two checks in the IF will always be true, so why are you checking them?
Re: Dynamic File Creation using UTL_FILE [message #650809 is a reply to message #650808] Wed, 04 May 2016 03:59 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

Thank you SOO much @cookiemonster. I modified the code based on your inputs for checking previous file num with current one and now its working. Could you also please advise which exceptions should I handle in my code? and also is there any way I can get the line number frim input file while trapping the errors? Here is the modified code:-

DECLARE

  w_dir                   VARCHAR2(100);
  w_input_file            VARCHAR2(50);


  w_newline               VARCHAR2(2000);
  w_NewPolNum             VARCHAR2(20);
  w_NewRecTyp                CHAR(2);
  w_NewTranDt             CHAR(5);

  w_TranTypCd             CHAR(2);
  w_file_num             NUMBER;

  input_file    utl_file.file_type;
  output_file   utl_file.file_type;
  w_Pol_Header number := 0;
  w_prev_file_num number :=0;
  
BEGIN
   dbms_output.enable(null);
   
    w_dir            := 'CENGROUP_OUTPUT'; 
    w_input_file     := 'wcpols_04152016_04222016_MI.txt';  
    --w_input_file     := 'wcpols_04152016_04222016_NCCI.txt';
    
    input_file    := utl_file.fopen(w_dir, w_input_file, 'r',32000);
  
    IF utl_file.is_open(input_file) THEN
     LOOP
     BEGIN

         utl_file.get_line(input_file, w_newline);
         w_NewPolNum  := substr(w_newline,6,14);
         w_NewRecTyp  := substr(w_newline,46,2);
         w_NewTranDt  := substr(w_newline,37,5);
         w_TranTypCd  := substr(w_newline,42,2);
         
         
         if w_NewPolNum = substr(w_newline,6,14) and w_NewTranDt = substr(w_newline,37,5) and w_NewRecTyp in ('01','08') then
           w_pol_header := w_pol_header + 1;     
         end if;
         
         select file_num
           into w_file_num
           from wcpols_submission_files
          where pol_header = w_pol_header;

          if w_file_num <> w_prev_file_num then
              utl_file.fclose(output_file);           
              output_file   := utl_file.fopen(w_dir, 'PPEP_MI_'||w_file_num||'.txt', 'a',32000);          
          end if;

          utl_file.put_line(output_file,w_file_num||'-'||w_NewPolNum||'-'||w_NewTranDt||'-'||w_TranTypCd||'-'||w_NewRecTyp);

      EXCEPTION WHEN NO_DATA_FOUND THEN
          EXIT;
       END;
       
      END LOOP; --main loop
      
    END IF;  --InputFile is_open

   w_prev_file_num := w_file_num;
   utl_file.fclose(input_file);
   utl_file.fclose(output_file);

END; 
Re: Dynamic File Creation using UTL_FILE [message #650813 is a reply to message #650809] Wed, 04 May 2016 05:11 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
You're setting w_prev_file_num in the wrong place.
It'll always be 0 until after the loop is finished and at that point it's value doesn't matter.
So it's closing and opening files on every iteration of the loop.

You need to set w_prev_file_num inside the loop.


If you want the line number of the input file I'd just maintain a counter variable inside the loop.
Re: Dynamic File Creation using UTL_FILE [message #650814 is a reply to message #650813] Wed, 04 May 2016 05:20 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

oh, thank you again. I put the w_prev_file_num inside the loop and it works much faster that the previous code. Because of its worng position it was taking time since it has to open and close each time. I really appreciate your time and efforts for this. I will check for getting line numbers. Thank you!!
Re: Dynamic File Creation using UTL_FILE [message #650815 is a reply to message #650814] Wed, 04 May 2016 05:48 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

when I run the code again, it simply appends all data again in the file. How I would restrict (probably after 2nd time run) the append if the file is already processed?
IF w_file_num <> w_prev_file_num THEN
     utl_file.fclose(output_file);      
     output_file := utl_file.fopen(w_dir,
                                   'PPEP_MI_' || w_file_num || '.txt',
                                    'a',
                                    32000);
end if;
Re: Dynamic File Creation using UTL_FILE [message #650822 is a reply to message #650815] Wed, 04 May 2016 08:59 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
I'm not sure exactly what you asking.
When you say "it simply appends all data again in the file" what exactly are you seeing?
Define already processed.
Re: Dynamic File Creation using UTL_FILE [message #650824 is a reply to message #650822] Wed, 04 May 2016 09:12 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Are you saying that if you run the whole block of code again it appends the data to files created by the first run?
If so then, obviously that will happen, but will it actually be run multiple times in the real system?
And if it is why aren't you either:
a) moving the files out the way once you're done
b) embedding a timestamp in the file name so each run generates different files.
Re: Dynamic File Creation using UTL_FILE [message #650825 is a reply to message #650824] Wed, 04 May 2016 10:22 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

Hello-
I am going to schedule this job and user will process the job. If user accidently process for 2nd time then that will append all records again. So just to avoid this situation, I am trying to check if output file is already exists there.

Also I am struggling to write one control record at the end of each file. could you please advise how I can check if the output file is written by all of its records and write a last record after that?

Thank you!
Re: Dynamic File Creation using UTL_FILE [message #650830 is a reply to message #650825] Wed, 04 May 2016 14:59 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Checking for an existing file is easy (pseudo code follows)

begin
output_file := utl_file.fopen(w_dir,'PPEP_MI_' || w_file_num || '.txt','r',32000);
utl_file.fclose(output_file);
-- skip to the next file. It already exists
when utl_file.invalid_operation then
   utl_file.fopen(w_dir,'PPEP_MI_' || w_file_num || '.txt','a',32000);
end;

Re: Dynamic File Creation using UTL_FILE [message #650837 is a reply to message #650830] Thu, 05 May 2016 02:42 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

Thank you Bill, I'll try for this later but now I need some input for this issue. The below code is generating 3 files (PPEP_1, PPEP_2 and PPEP_3) based on file_num from wcpos_submission_files table. I need to print the '9999' record at the end of each files (1,2 and 3) but this code is writing just on files 2 and 3. Its not writing on file 1. I am trying to write this record at last line when file num chages but its not working as desired. Please help to locate my mistake.
DECLARE

  w_dir        VARCHAR2(100);
  w_input_file VARCHAR2(50);
  w_newline           VARCHAR2(2000);
  w_NewPolNum         VARCHAR2(20);
  w_NewRecTyp         CHAR(2);
  w_NewTranDt         CHAR(5);
  w_LinkRec           VARCHAR2(100);
  w_StateCd           CHAR(2);
  w_RecTypCd          CHAR(2);
  w_ExpRtgCancReinsCd CHAR(1);
  w_CancTypCd         CHAR(1);
  w_ResnCancCd        CHAR(2);
  w_EmpLeasCd         CHAR(1);
  w_ExpModCd          CHAR(1);
  w_OutRecord         VARCHAR2(301);  
  w_if_NCCI           NUMBER;
  w_TranTypCd CHAR(2);
  w_file_num  NUMBER;
  input_file      utl_file.file_type;
  output_file     utl_file.file_type;
  w_Pol_Header    number := 0;
  w_prev_file_num number := 0;

BEGIN
  dbms_output.enable(null);
  w_dir        := 'CENGROUP_OUTPUT';
  w_input_file := 'wcpols_04152016_04222016_MI.txt';
  w_if_NCCI := instr(w_input_file,'NCCI');  

  input_file := utl_file.fopen(w_dir, w_input_file, 'r', 32000);

  IF utl_file.is_open(input_file) THEN
    LOOP
      BEGIN      
        utl_file.get_line(input_file, w_newline);
        IF substr(w_newline, 46, 2) <> '99' THEN
          w_NewPolNum := substr(w_newline, 6, 14);
          w_NewRecTyp := substr(w_newline, 46, 2);
          w_NewTranDt := substr(w_newline, 37, 5);
          w_TranTypCd := substr(w_newline, 42, 2);
        
          IF w_NewPolNum = substr(w_newline, 6, 14) AND
             w_NewTranDt = substr(w_newline, 37, 5) AND
             w_NewRecTyp in ('01', '08') then
             w_pol_header := w_pol_header + 1;
          END IF;
        
          SELECT file_num
            INTO w_file_num
            FROM wcpols_submission_files
           WHERE pol_header = w_pol_header;
        
          IF w_file_num <> w_prev_file_num THEN
            
            if utl_file.is_open(output_file) then --i'm assuming at this location it'll write 9999 at each file. 
              utl_file.put_line(output_file, '9999');--but its skipping file 1. (writing just on 2 and 3)
            end if;
            
            utl_file.fclose(output_file);

            output_file := utl_file.fopen(w_dir,
                                          'PPEP_MI_' || w_file_num || '.txt',
                                          'a',
                                          32000);
            IF w_if_NCCI > 0 THEN
              utl_file.put_line(output_file, 'This is NCCI');
            END IF;            
          END IF;           
          utl_file.put_line(output_file, w_newline);                  
        END IF; --99
        
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
          EXIT;
      END;      
      w_prev_file_num := w_file_num;     
    END LOOP; --main loop
  END IF; --InputFile is_open

  utl_file.fclose(input_file);  
  utl_file.fclose(output_file);

END;
Re: Dynamic File Creation using UTL_FILE [message #650847 is a reply to message #650837] Thu, 05 May 2016 07:45 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

I missed to mention that the last file written (based on file_num from wcpols table) is file 1. The code writes in this order:-
1) file 1
2) file 2
3) file 3
4) file 1

So the 9999 record as a last line of each file is written on 2 and 3 but not on the last file i.e. file 1 in this case. Using dbms_output.put_line, I figured that the files are appended with above sequence correctly but using utl_file its not writing on file 1 (as a last file)
Please advise me where I am mistaking.

       IF w_file_num <> w_prev_file_num THEN
                     
            [color=red]IF  utl_file.is_open(output_file) THEN                                      
               utl_file.put_line(output_file, '9999'||w_line_count||CHR(13));                
            END IF;            [/color]
            
            utl_file.fclose(output_file);
            w_line_count := 0;
            output_file := utl_file.fopen(w_dir,'PPEP_MI_' || w_file_num || '.txt','a',32000);
            
            IF w_if_NCCI > 0 THEN                            
              utl_file.put_line(output_file, 'this is NCCI');
              w_line_count := w_line_count + 1;
            END IF;            
          END IF;           
          
          utl_file.put_line(output_file, w_newline); 
          w_line_count := w_line_count + 1;  
                        

Re: Dynamic File Creation using UTL_FILE [message #650848 is a reply to message #650847] Thu, 05 May 2016 07:48 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
You need to add the code

utl_file.put_line(output_file, '9999');

just before your close your input and output file after the end loop

 END LOOP; --main loop
  END IF; --InputFile is_open
  utl_file.put_line(output_file, '9999');
  utl_file.fclose(input_file);  
  utl_file.fclose(output_file);

[Updated on: Thu, 05 May 2016 07:49]

Report message to a moderator

Re: Dynamic File Creation using UTL_FILE [message #650849 is a reply to message #650848] Thu, 05 May 2016 07:54 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

I have tried that too but in that case the 9999 record is being written just on file 1 and not in 2 and 3. Also I need to print the line counts too so I could not use the w_line_count variable if I put this line after end loop. Please advise. Here is the code with this change-
DECLARE

  w_dir			VARCHAR2(100);
  w_input_file		VARCHAR2(50);
  w_newline		VARCHAR2(2000);
  w_NewPolNum		VARCHAR2(20);
  w_NewRecTyp		CHAR(2);
  w_NewTranDt		CHAR(5);
  w_RecTypCd		CHAR(2);
  w_if_NCCI		NUMBER;
  w_TranTypCd		CHAR(2);
  w_file_num		NUMBER;
  input_file		utl_file.file_type;
  output_file		utl_file.file_type;
  w_Pol_Header		NUMBER := 0;
  w_prev_file_num	NUMBER := 0;
  w_line_count		NUMBER := 0; 
  
  w_FileSt		VARCHAR(4);
  w_Lable		CHAR(14);
  w_Email		CHAR(31);
  w_DataTypCd    CHAR(3);
  w_DataRecCd    CHAR(5);
  w_VersId    CHAR(8);
  w_DataPrvdr    CHAR(5) := '14095';
 

BEGIN
  dbms_output.enable(null);
  w_dir        := 'CENGROUP_OUTPUT';
  w_input_file := 'wcpols_04152016_04222016_MI.txt';
  w_if_NCCI := instr(w_input_file,'MI');  
  w_FileSt := substr(substr(w_input_file,26),1,(instr(substr(w_input_file,26),'.txt')-1));

  input_file := utl_file.fopen(w_dir, w_input_file, 'r', 32000);

  IF utl_file.is_open(input_file) THEN
    LOOP
      BEGIN      
        utl_file.get_line(input_file, w_newline);
        IF substr(w_newline, 46, 2) <> '99' THEN
          w_NewPolNum := substr(w_newline, 6, 14);
          w_NewRecTyp := substr(w_newline, 46, 2);
          w_NewTranDt := substr(w_newline, 37, 5);
          w_TranTypCd := substr(w_newline, 42, 2);
        
          IF w_NewPolNum = substr(w_newline, 6, 14) AND
             w_NewTranDt = substr(w_newline, 37, 5) AND
             w_NewRecTyp in ('01', '08') then
             w_pol_header := w_pol_header + 1;
          END IF;
        
          SELECT file_num
            INTO w_file_num
            FROM wcpols_submission_files
           WHERE pol_header = w_pol_header;
        
          IF w_file_num <> w_prev_file_num THEN
                     
           /* IF  utl_file.is_open(output_file) THEN                                      
               utl_file.put_line(output_file, '9999'||w_line_count||CHR(13));                
            END IF;  */          
            
            utl_file.fclose(output_file);
            w_line_count := 0;
            output_file := utl_file.fopen(w_dir,'PPEP_MI_' || w_file_num || '.txt','a',32000);
            
            IF w_if_NCCI > 0 THEN
              
              utl_file.put_line(output_file, 'This is NCCI');
              w_line_count := w_line_count + 1;
            END IF;            
          END IF;           
          
          utl_file.put_line(output_file, w_newline); 
          w_line_count := w_line_count + 1;  
                        
        END IF; --99
        
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
          EXIT;
      END;      
      w_prev_file_num := w_file_num;  
      
      
      
    END LOOP; --main loop
  END IF; --InputFile is_open
  utl_file.put_line(output_file, '9999'||/*w_line_count||*/CHR(13));   
  utl_file.fclose(input_file);  
  utl_file.fclose(output_file);

END;

Re: Dynamic File Creation using UTL_FILE [message #650850 is a reply to message #650849] Thu, 05 May 2016 07:55 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
You don't move the other line. It has to be in both places. The first inserts when the file number changes, the second inserts when the input file ends. Of course you can use w_line_count. It is still valid outside the loop.

[Updated on: Thu, 05 May 2016 08:07]

Report message to a moderator

Re: Dynamic File Creation using UTL_FILE [message #650853 is a reply to message #650850] Thu, 05 May 2016 08:06 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

Oh!!! Its now on all the 3 pages when I keep the line at both places but the lien count is not correct for file 1 since its after the main end loop. I will research for this. Thank you so much Bill for your help!!!
Re: Dynamic File Creation using UTL_FILE [message #650882 is a reply to message #650853] Fri, 06 May 2016 01:41 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

I am facing another issue while writing the files. E.g. the input file contains the following records and based on validations it should go to specific file.
rec1 this should go into file 1
rec2 this should go into file 1
rec3 this should go into file 2
rec4 this should go into file 3
rec5 this should go into file 1

All the 3 output files should contain 'This is NCCI' line at the top line and total line_count at the last line. The below code generates the 3 files but in the file 1 there are 2 'This is NCCI' at the top of rec 1 and rec2 becuase I put the code in 'IF w_file_num <> w_prev_file_num THEN' this line is to check which file the record should go. Actually it should write 'This is NCCI' just for 1 time at top of file 1. Also the line_count is for coming for each record. When rec1 and rec5 is written in file1 then line count should be 2 and printed at last of rec5 but now its coming at last of both records and invidual line count of a record. I am not sure where should I put the code so it should treat the rec5 as the part of rec1 as its being written on file1 itself and not at a separate file. Please advise your thoughts so I could modify accordingly.

say the desired output is
File 1
---------
This is NCCI
rec1
rec5
99992
but currently its writing as
File 1
---------
This is NCCI
rec1
99991
This is NCCI
rec5
99991

DECLARE

  w_dir			VARCHAR2(100);
  w_input_file		VARCHAR2(50);
  w_newline		VARCHAR2(2000);
  w_NewPolNum		VARCHAR2(20);
  w_NewRecTyp		CHAR(2);
  w_NewTranDt		CHAR(5);
  w_RecTypCd		CHAR(2);
  w_if_Not_NCCI		NUMBER;
  w_TranTypCd		CHAR(2);
  w_file_num		NUMBER;
  input_file		utl_file.file_type;
  output_file		utl_file.file_type;
  w_Pol_Header		NUMBER := 0;
  w_prev_file_num	NUMBER := 0;
  w_line_count		NUMBER := 0; 

BEGIN
  dbms_output.enable(null);
  w_dir        := 'CENGROUP_OUTPUT';
  w_input_file := 'wcpols_04152016_04222016_MI.txt';
  w_if_Not_NCCI := instr(w_input_file,'NCCI');  
  w_FileSt := substr(substr(w_input_file,26),1,(instr(substr(w_input_file,26),'.txt')-1));

  input_file := utl_file.fopen(w_dir, w_input_file, 'r', 32000);

  IF utl_file.is_open(input_file) THEN
    LOOP
      BEGIN      
        utl_file.get_line(input_file, w_newline);
        IF substr(w_newline, 46, 2) <> '99' THEN
          w_NewPolNum := substr(w_newline, 6, 14);
          w_NewRecTyp := substr(w_newline, 46, 2);
          w_NewTranDt := substr(w_newline, 37, 5);
          w_TranTypCd := substr(w_newline, 42, 2);
        
          IF w_NewPolNum = substr(w_newline, 6, 14) AND
             w_NewTranDt = substr(w_newline, 37, 5) AND
             w_NewRecTyp in ('01', '08') then
             w_pol_header := w_pol_header + 1;
          END IF;
        
          SELECT file_num
            INTO w_file_num
            FROM wcpols_submission_files
           WHERE pol_header = w_pol_header;
        
          IF w_file_num <> w_prev_file_num THEN
                     
            IF  utl_file.is_open(output_file) THEN                                      
               utl_file.put_line(output_file, '9999'||w_line_count||CHR(13)); -- this is writing for rec1 and rec5 separately
            END IF;            
            
            utl_file.fclose(output_file);
            w_line_count := 0;
            output_file := utl_file.fopen(w_dir,'PPEP_MI_' || w_file_num || '.txt','a',32000);
            
            IF w_if_Not_NCCI = 0 THEN
              utl_file.put_line(output_file, 'This IS NCCI'||CHR(13)); -- this is writing for rec1 and rec5 separately
              w_line_count := w_line_count + 1;
              
            END IF;            
          END IF;           
          
          utl_file.put_line(output_file, w_newline); 
          w_line_count := w_line_count + 1;  
                        
        END IF; --99
        
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
          EXIT;
      END;      
      w_prev_file_num := w_file_num;      
      
    END LOOP; --main loop
  END IF; --InputFile is_open
  utl_file.put_line(output_file, '9999'||w_line_count||CHR(13));   
  utl_file.fclose(input_file);  
  utl_file.fclose(output_file);

END;

Re: Dynamic File Creation using UTL_FILE [message #650883 is a reply to message #650882] Fri, 06 May 2016 02:34 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

I used utl_file.fgetattr to check if file size is more than zero and if its 0 then print 'This is NCCI' so now this line is coming at the top of file 1 just for 1 time and not on every time the record changes. BUT still the line count is not correct. For file1 its coming as
File 1
-------
This is NCCI
rec1
99991
rec5
99991

it should be
File 1
-------
This is NCCI
rec1
rec5
99992

Any thoughts on how I should modify the code? I tried to set w_line_count := 0 IF l_size = 0 but it didn't give the correct line count. The line count should be the total for rec1 and rec5 lines. Please assist.


DECLARE

  w_dir      VARCHAR2(100);
  w_input_file    VARCHAR2(50);
  w_newline    VARCHAR2(2000);
  w_NewPolNum    VARCHAR2(20);
  w_NewRecTyp    CHAR(2);
  w_NewTranDt    CHAR(5);
  w_RecTypCd    CHAR(2);
  w_if_Not_NCCI    NUMBER;
  w_TranTypCd    CHAR(2);
  w_file_num    NUMBER;
  input_file    utl_file.file_type;
  output_file    utl_file.file_type;
  w_Pol_Header    NUMBER := 0;
  w_prev_file_num  NUMBER := 0;
  w_line_count    NUMBER := 0; 
  
  l_exists boolean;
  l_size number;
  l_block_size number;

 

BEGIN
  dbms_output.enable(null);
  w_dir        := 'CENGROUP_OUTPUT';
  w_input_file := 'wcpols_04152016_04222016_MI.txt';
  w_if_Not_NCCI := instr(w_input_file,'NCCI');  
  w_FileSt := substr(substr(w_input_file,26),1,(instr(substr(w_input_file,26),'.txt')-1));

  input_file := utl_file.fopen(w_dir, w_input_file, 'r', 32000);

  IF utl_file.is_open(input_file) THEN
    LOOP
      BEGIN      
        utl_file.get_line(input_file, w_newline);
        IF substr(w_newline, 46, 2) <> '99' THEN
          w_NewPolNum := substr(w_newline, 6, 14);
          w_NewRecTyp := substr(w_newline, 46, 2);
          w_NewTranDt := substr(w_newline, 37, 5);
          w_TranTypCd := substr(w_newline, 42, 2);
        
          IF w_NewPolNum = substr(w_newline, 6, 14) AND
             w_NewTranDt = substr(w_newline, 37, 5) AND
             w_NewRecTyp in ('01', '08') then
             w_pol_header := w_pol_header + 1;
          END IF;
        
          SELECT file_num
            INTO w_file_num
            FROM wcpols_submission_files
           WHERE pol_header = w_pol_header;
        
          IF w_file_num <> w_prev_file_num THEN
                     
            IF  utl_file.is_open(output_file) THEN                                      
               utl_file.put_line(output_file, '9999'||w_line_count||CHR(13));                
            END IF;            
            
            utl_file.fclose(output_file);
            w_line_count := 0;
            output_file := utl_file.fopen(w_dir,'PPEP_MI_' || w_file_num || '.txt','a',32000);
            
            utl_file.fgetattr( w_dir, 'PPEP_MI_'||w_file_num||'.txt', l_exists, l_size, l_block_size );
            
            
            
            IF w_if_Not_NCCI = 0 and l_size = 0 THEN --if file is new then print this line
               utl_file.put_line(output_file, 'This is NCCI'CHR(13));
              w_line_count := w_line_count + 1;
              
            END IF;            
          END IF;           
          
          utl_file.put_line(output_file, w_newline); 
          w_line_count := w_line_count + 1;  
                        
        END IF; --99
        
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
          EXIT;
      END;      
      w_prev_file_num := w_file_num;      
      
    END LOOP; --main loop
  END IF; --InputFile is_open
  utl_file.put_line(output_file, '9999'||w_line_count||CHR(13));   
  utl_file.fclose(input_file);  
  utl_file.fclose(output_file);

END;

Re: Dynamic File Creation using UTL_FILE [message #650884 is a reply to message #650883] Fri, 06 May 2016 02:42 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
You really should follow my initial suggestion of using an external table for the input file.
This has the big advantage of letting you order the data as it is read from the input file so you can ensure data is read in the order it should be written to the other files.
This makes it trivally easy to know:
a) when you're at the start of a file
b) when you're at the end of a file
c) How many records you've written to the file.
Re: Dynamic File Creation using UTL_FILE [message #650885 is a reply to message #650884] Fri, 06 May 2016 02:44 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
If you don't do that the only way to get correct line counts is to have an array that stores file number (or name) and line count.
Then after you've written all lines to all files you can loop over the array and append the correct line count to each file.
Re: Dynamic File Creation using UTL_FILE [message #650886 is a reply to message #650885] Fri, 06 May 2016 03:08 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

Hi Cookiemonster,

Actually I have created a database table wcpols_submission_files with all the distinct record header and the file_num column is updated that which record should go in which file no. The input file has say 7k line of data and all are associated with their header record. I have captured only the header records in this table and I am updating file num based of which header should go in which file.

for example, the input file lay out:-
A --> This is 1st header record
a1
a2
a3
.
.
B --> This is 2nd header record
b1
b2
b3
.
.
like so on up to 11 policy header and its associated data.

In current code I open the input file then check the file num from db table then writing in the respective file but the case come that some records may go to very first file and that time the line count is failing. Could you please advise which approach should I follow? Thank you for your time on this.
Re: Dynamic File Creation using UTL_FILE [message #650887 is a reply to message #650886] Fri, 06 May 2016 03:28 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

When we had discussed previously, I was getting data from cursor queries but now in this case all the data is coming from input file. I just have to read the input file and then decide which file the records should go. The records are not sorted in input file so I created the db table to store file num and file num may be in order so I need to append the data as per the file nums from db table (which I am populating while reading the input file with another code)then at each out put file generated I need to put first line and line outs at last line of each file. I would be really appreciative if you could advise any better approach than this or any corrections with my existing code.

Thank you!
Re: Dynamic File Creation using UTL_FILE [message #650888 is a reply to message #650887] Fri, 06 May 2016 03:54 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
How many times do I have to say "use an external table" before you actually try it?

I get the feeling you don't know what external tables are, if so - have a read in the documentation. They are the perfect solution to all the problems you are facing.
Re: Dynamic File Creation using UTL_FILE [message #650889 is a reply to message #650888] Fri, 06 May 2016 05:05 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

I'll try using external tables and will post here, thanks!
Re: Dynamic File Creation using UTL_FILE [message #650890 is a reply to message #650889] Fri, 06 May 2016 05:22 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

The problem I may face that I couldn't define the column name while creating table because the input file's data is inconsistent in position of column because the input file looks like this
14095WC927988221754     0000001501201611110  015         1601200000000000111WC927868221754    000000     0312       00000010001200000017780000002178113000100000000010000000001000000TRIDENT INSURANCE SERVICES, LL       0 00000                  000000                                       000000000000
14095WC927988221754     0000001501201611110  022001 Pro Motion Inc                                                                                  4317190490010322320382500000   29431719049000   380001829785                                       01                    00                 000000000000
14095WC927988221754     0000001501201611110  022001 Pro Motion Inc                                                                                  43171904900103431719049                                                                            02                    00                 000000000000
14095WC927988221754     0000001501201611110  031N118405 Edison Ave                                            Chesterfield                  MO63005    0012400001                         0000000000000000561499                                                            00                  000000000000
14095WC927988221754     0000001501201611110  033N1225 West Washington Street, 24th Floor                      Chicago                       IL60606    9999999999                                   000000                                                                  00                  000000000000
14095WC927988221754     0000001501201611110  035N110101 Reunion Place, Suite 450                              San Antonio                   TX78216    9999999999                                   000000                                                                  00                  000000000000
14095WC927988221754     0000001501201611110  0622433344649000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000                                                                                                                                            000000000000
14095WC927988221754     00000015012016111100007   WC000000C           WC000001A           WC000303                                                                                                                                                                                              000000000000
14095WC927988221754     00000015012016111100107   AG005               WC000404            WC000414            WC000419            WC000421D           WC000422B           WC890600B           WC890415                                                                                          000000000000
14095WC927988221754     00000015012016111100207   AG005               WC000404            WC000414            WC000421D           WC000422B           WC020401C           WC020601            WC890600B           WC890415                                                                      000000000000
14095WC927988221754     00000015012016111100307   AG005               WC000404            WC000414            WC000419            WC000421D           WC000422B           WC030601A           WC890600B           WC890415                                                                      000000000000
14095WC927988221754     00000015012016111100407   PN049902B           PN049903            PN049904            PN049906            WC000404            WC000419            WC000421D           WC000422B           WC040301C           WC040310            WC040360B                             000000000000
14095WC927988221754     00000015012016111100407   WC040422            WC040601A           WC890600B           WC890415            WC990491                                                                                                                                                      000000000000
14095WC927988221754     00000015012016111100507   AG005               WC000404            WC000414            WC000419            WC000421D           WC000422B           WC050402            WC890600B           WC890415                                                                      000000000000
14095WC927988221754     00000015012016111100607   AG005               Form061 C           WC000404            WC000414            WC000419            WC000421D           WC000422B           WC060301            WC060303C           WC060601            WC890600B                             000000000000
14095WC927988221754     00000015012016111100607   WC890415                                                                                                                                                                                                                                      000000000000
14095WC927988221754     00000015012016111100807   AG005               WC000404            WC000414            WC000419            WC000421D           WC000422B           WC080601            WC890600B           WC890415                                                                      000000000000
14095WC927988221754     00000015012016111100907   AG005FL             Form094 C           WC000404            WC000414            WC000419            WC090303 
Re: Dynamic File Creation using UTL_FILE [message #650891 is a reply to message #650890] Fri, 06 May 2016 05:23 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

its not always the fix position format for each record, so even I define the columns the data that would go inside external table would not guarantee for correct values.
Re: Dynamic File Creation using UTL_FILE [message #650894 is a reply to message #650891] Fri, 06 May 2016 05:33 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Well if that's true how is your current approach going to work?

If you can write a statement that just uses substr to get the data you want from each line then you can create an external table that does the same.
Re: Dynamic File Creation using UTL_FILE [message #650895 is a reply to message #650894] Fri, 06 May 2016 05:45 Go to previous messageGo to next message
harshadsp
Messages: 100
Registered: August 2008
Senior Member

But if I dump the entire record from input file in just one column then I need to use the same logic with that table as I am using while input file. When I use substr just to get the header line of the main records and then populating wcpols table and based on headers I am updating file num in that table.

I think I should try to create a associative array and then extend it when I create a new file and then store the previous processed file in that array and then while writing the 9999 record, check if the array has that file num processed, and if not then print the last line. I'm still not sure if this will work as per expected but will try for this. Do you think its advisable?
Thanks!
Re: Dynamic File Creation using UTL_FILE [message #650907 is a reply to message #650895] Fri, 06 May 2016 08:01 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
To do the array approach you need to add a new entry to the array each time you use a file number you've not used before then update the count for that file for each line written to it.
Then after you've finished looping over the input file, so after the loop, loop over the array and append the 9999 line to each file in turn.
You can't do this inside the existing loop as you can't know that you're not going to encounter more rows for the current file later on.
Re: Dynamic File Creation using UTL_FILE [message #650908 is a reply to message #650895] Fri, 06 May 2016 08:01 Go to previous messageGo to previous message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
No, an external table allows oracle to access a specifically named flat file as if it was an oracle table.
so you could do a simple ordered query on the external table to split it into the different files. Very easy to do.
See the following link on how to use it.

https://oracle-base.com/articles/9i/external-tables-9i

If you are running Oracle 12, there is a link showing enhancements available for external tables in the page.

[Updated on: Fri, 06 May 2016 08:02]

Report message to a moderator

Previous Topic: Table Partitioning:
Next Topic: Convert local time to GMT using off set
Goto Forum:
  


Current Time: Tue Apr 23 23:38:40 CDT 2024