
CREATE OR REPLACE 
PACKAGE svecchiamento_dwh
  IS

  PROCEDURE old_mnp_report_agcom (path_file in varchar2, anno_mese in varchar2);
     
 
  
END; -- Package spec
/


CREATE OR REPLACE 
PACKAGE BODY svecchiamento_dwh
IS

PROCEDURE old_mnp_report_agcom (path_file in varchar2, anno_mese in varchar2)
   IS

puntatore_file   SYS.UTL_FILE.FILE_TYPE;
puntatore_file_log   SYS.UTL_FILE.FILE_TYPE;
tutto         		varchar2(1000);
sql_msg         	varchar2(1000);
sql_code            integer;
nome_file           varchar2(100);
n_rec_in            integer;
n_rec_fin           integer;
n_rec_del           integer;

clob_loc          CLOB;
  buffer            VARCHAR2(32767);
  buffer_size       CONSTANT BINARY_INTEGER := 32767;
  amount            BINARY_INTEGER;
  offset            NUMBER(38);

  file_handle       UTL_FILE.FILE_TYPE;
  directory_name    CONSTANT VARCHAR2(80) := path_file;
  --csv_filename  CONSTANT VARCHAR2(80) := '';
  csv_filename  VARCHAR2(80);

CURSOR cur_rep_agcom is
   SELECT a.anno||'|'||a.mese||'|'||a.tipo||'|'||a.data_creazione||'|',
   path_file||'/agcom_'||a.anno||'_'||a.mese||'_'||a.tipo||'_'||to_char(a.data_creazione,'yyyymmdd')||'.csv', 
   a.csv
  FROM mnp_report_agcom a
  WHERE anno||mese = anno_mese;

BEGIN

DBMS_OUTPUT.put_line ('Inizio Procedura old_mnp_report_agcom '||to_char(sysdate, 'dd/mm/yyyy hh24:mi:ss'));

--puntatore_file := UTL_FILE.FOPEN('E:\Oracle\oradata','prova.txt','w');

  puntatore_file_log := UTL_FILE.FOPEN(path_file,'old_mnp_report_agcom_'||to_char(sysdate, 'ddmmyyyy_hh24miss')||'.log','w'); 
 
   SYS.UTL_FILE.PUT_LINE(puntatore_file_log, 'Inizio Procedura old_mnp_report_agcom '||to_char(sysdate, 'dd/mm/yyyy hh24:mi:ss')); 
   
  SYS.UTL_FILE.PUT_LINE(puntatore_file_log, 'Apertura file old_mnp_report_agcom_'||anno_mese||'.dat');
 
  puntatore_file := UTL_FILE.FOPEN(path_file,'old_mnp_report_agcom_'||anno_mese||'.dat','w'); 

  SYS.UTL_FILE.PUT_LINE(puntatore_file_log, 'Inizio scrittura file old_mnp_report_agcom_'||anno_mese||'.dat');

	OPEN cur_rep_agcom;

   LOOP
      FETCH cur_rep_agcom
      INTO
        tutto, csv_filename, clob_loc;

      EXIT WHEN cur_rep_agcom%NOTFOUND;

 
 
SYS.UTL_FILE.PUT_LINE(puntatore_file, tutto||csv_filename||'|');

 SYS.UTL_FILE.PUT_LINE(puntatore_file_log, 'Inizio export file '||csv_filename);
 
 ----------------------------
 ----------------------------
  DBMS_OUTPUT.ENABLE(100000);

    -- ----------------
    -- GET CLOB LOCATOR
    -- ----------------
    /*SELECT csv INTO clob_loc
    FROM   mnp_report_agcom
    where rownum < 2;*/


    -- --------------------------------
    -- OPEN NEW XML FILE IN WRITE MODE
    -- --------------------------------
    file_handle := UTL_FILE.FOPEN(
        location     => directory_name,
        --filename     => new_xml_filename||replace(tutto,'|','_')||'.csv',
        filename     => csv_filename,
        open_mode    => 'w',
        max_linesize => buffer_size);

    amount := buffer_size;
    offset := 1;

    -- ----------------------------------------------
    -- READ FROM CLOB XML / WRITE OUT NEW XML TO DISK
    -- ----------------------------------------------
    WHILE amount >= buffer_size
    LOOP

        DBMS_LOB.READ(
            lob_loc    => clob_loc,
            amount     => amount,
            offset     => offset,
            buffer     => buffer);

        offset := offset + amount;

        UTL_FILE.PUT(
            file      => file_handle,
            buffer    => buffer);

       UTL_FILE.FFLUSH(file => file_handle);

    END LOOP;

    UTL_FILE.FCLOSE(file => file_handle);
    
     SYS.UTL_FILE.PUT_LINE(puntatore_file_log, 'Fine export file '||csv_filename);
----------------------------------------------
----------------------------------------------
    
   END LOOP;
    CLOSE cur_rep_agcom; 
    
    SYS.UTL_FILE.PUT_LINE(puntatore_file_log, 'Fine scrittura file old_mnp_report_agcom_'||anno_mese||'.dat');   
    
    SYS.UTL_FILE.FCLOSE(puntatore_file);
    
  SYS.UTL_FILE.PUT_LINE(puntatore_file_log, 'Chiusura file old_mnp_report_agcom_'||anno_mese||'.dat');    
  
   SYS.UTL_FILE.PUT_LINE(puntatore_file_log, 'Inizio Delete tabella mnp_report_agcom');
   
   select count(*) into n_rec_in from mnp_report_agcom where anno||mese = anno_mese;
   
   delete from mnp_report_agcom where anno||mese = anno_mese;
   commit;
   
   select count(*) into n_rec_fin from mnp_report_agcom where anno||mese = anno_mese;
   
   n_rec_del := n_rec_in - n_rec_fin;
   
    SYS.UTL_FILE.PUT_LINE(puntatore_file_log, 'Fine Delete tabella mnp_report_agcom, eliminati '||n_rec_del||' record');
     
    	sql_msg := sqlerrm;
        sql_code := sqlcode;
        
SYS.UTL_FILE.PUT_LINE(puntatore_file_log, 'Fine Procedura old_mnp_report_agcom '||to_char(sysdate, 'dd/mm/yyyy hh24:mi:ss')||' sqlmsg: '||sql_msg||' - sqlcode: '||sql_code); 
SYS.UTL_FILE.FCLOSE(puntatore_file_log);
DBMS_OUTPUT.put_line ('Fine Procedura old_mnp_report_agcom '||to_char(sysdate, 'dd/mm/yyyy hh24:mi:ss')||' sqlmsg: '||sql_msg||' - sqlcode: '||sql_code);
    
EXCEPTION
      WHEN OTHERS  THEN       
    	sql_msg := sqlerrm;
        sql_code := sqlcode;
        ROLLBACK;
        SYS.UTL_FILE.PUT_LINE(puntatore_file_log, 'Procedura old_mnp_report_agcom terminata con errore '||to_char(sysdate, 'dd/mm/yyyy_hh24:mi:ss')||' sqlmsg: '||sql_msg||' - sqlcode: '||sql_code); 
          SYS.UTL_FILE.FCLOSE(puntatore_file_log);
        DBMS_OUTPUT.put_line ('Procedura old_mnp_report_agcom terminata con errore '||to_char(sysdate, 'dd/mm/yyyy_hh24:mi:ss')||' sqlmsg: '||sql_msg||' - sqlcode: '||sql_code);

END; -- Procedure




END;
/


