declare 
cursor cur is
  select b.codice_richiesta_recipient || ';' || b.msisdn
  from mnp_gestione_richiesta_cess b
  where b.stato in(2,3)  
  and b.tim_donor =1;

INDICE NUMBER;
SECONDI NUMBER;
file1_cess 		UTL_FILE.file_type;
file1_ctr  		UTL_FILE.file_type;
path   		varchar2(255);
filename    varchar2(255);
filename_cess    varchar2(255);
filename_ctr    varchar2(255);
rs      varchar2(255);

BEGIN 
	  INDICE:=0;
	  SECONDI:=0;
	  select directory_path into path from all_directories
 	  where directory_name='PATH_REPORT_CESS';
 	  filename := 'CESSAZIONI_'||to_char(sysdate,'yyyyMMddhh24mi')||TRIM(TO_CHAR(SECONDI,'09'));
	  filename_cess := filename||'.txt';
	  filename_ctr := filename||'.ctr';
 	  file1_cess := UTL_FILE.fopen ('PATH_REPORT_CESS',filename_cess, 'w');
	  file1_ctr := UTL_FILE.fopen ('PATH_REPORT_CESS',filename_ctr, 'w');
 
	  
	  OPEN CUR;
	  LOOP
	  FETCH CUR INTO rs;
	  EXIT WHEN CUR%NOTFOUND;
	  	   
		   IF(MOD(INDICE,50000)=0 AND INDICE<>0) THEN		   
		   UTL_FILE.fflush   (file1_cess);
    	   UTL_FILE.fclose   (file1_cess);
		   UTL_FILE.fclose   (file1_ctr);
		   SECONDI:=SECONDI+1;
 		   filename := 'CESSAZIONI_'||to_char(sysdate,'yyyyMMddhh24mi')||TRIM(TO_CHAR(SECONDI,'09'));
 		   filename_cess := filename||'.txt';
	  	   filename_ctr := filename||'.ctr';
 	  	   file1_cess := UTL_FILE.fopen ('PATH_REPORT_CESS',filename_cess, 'w');
	  	   file1_ctr := UTL_FILE.fopen ('PATH_REPORT_CESS',filename_ctr, 'w');		  		   
	 	   END IF;
		   
		   UTL_FILE.put_line (file1_cess,rs);
			
	  INDICE:=INDICE+1;
	  END LOOP;
	  CLOSE CUR;
	  UTL_FILE.fflush   (file1_cess);
      UTL_FILE.fclose   (file1_cess);
	  UTL_FILE.fclose   (file1_ctr);
END;
/ 