Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Importieren einer CSV-Datei in eine Oracle Forms-Anwendung

Ich habe jetzt eine passende Lösung für mich gefunden. Vielleicht könnte ich es besser machen, aber das hilft mir soweit.

Ich habe folgenden Blog gefunden:http://tfathy.blogspot.de/2009/03/reading-from-file.html

Der Code funktioniert zu 100% per Copy&Paste und hilft mir fortan, die Aufgabe zu erledigen.

Vielleicht hilft es auch jemand anderem.

Hier ist der Lösungscode:

Reading From File 
March 19, 2009
--------------------------------------------------
Declare
  vfilename varchar2(500);
  in_file   Client_Text_IO.File_Type;
  linebuf   VARCHAR2(1800); 
BEGIN
    vfilename := client_get_file_name('c:/temp/', File_Filter=>'Comma Dialimeted Files (*.csv)|*.csv|'); 
    in_file := client_Text_IO.Fopen(vfilename, 'r');  
    GO_BLOCK('Emp'); 
    FIRST_RECORD;  
  LOOP
    Client_Text_IO.Get_Line(in_file, linebuf); 
    p_output_line(linebuf);
    Client_Text_IO.New_Line; 
    Next_record; 
  END LOOP; 
   FIRST_RECORD;
EXCEPTION
  WHEN no_data_found THEN
    Client_Text_IO.Put_Line('Closing the file...');
    Client_Text_IO.Fclose(in_file);
END;
-------------------------------------------------------
PROCEDURE p_output_line(p_line varchar2) IS 
vLINE VARCHAR2(4000);
vVALUE VARCHAR2(1000); 
vCOMMA_COUNT NUMBER;
vREPORT_DATE DATE;
BEGIN                    
 vLINE := p_line;
 vCOMMA_COUNT := LENGTH(vLINE)- LENGTH(REPLACE(vLINE,',','')); -- COUNT THE NUMBER OF COMMAS
  FOR I IN 1.. vCOMMA_COUNT+1 LOOP  
   vVALUE := SUBSTR(vLINE,1,INSTR(vLINE,',')-1);                             -- IF vLINE = 123,ABC,9877 THEN VVALUE WILL BE  123
    IF vVALUE IS NULL THEN
        vVALUE := vLINE;
    END IF;    
   vLINE := SUBSTR(vLINE,INSTR(vLINE,',')+1) ;                              -- CHANGE   123,ABC,9877 TO BE   ABC,9877  
   IF I = 1 THEN 
    :DATA.BMK_NAME := vVALUE; 
   ELSIF I = 2 THEN 
    vREPORT_DATE := last_day(to_date(vVALUE,'dd-mm-yyyy')); 
    :DATA.REPORT_DATE := vREPORT_DATE;
   ELSIF I = 3 THEN                 
    :DATA.BMK_RETURN := to_number(vVALUE);
   END IF;
  END LOOP; 
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
    MESSAGE('Please Check the data type is appropriate on you excel file');
    MESSAGE('Please Check the data type is appropriate on you excel file');
END; 
-----------------------------------------------------------------------
-- notes
1- you must install webutil version 106 or later
2- make sure that you attached and compiled the webutill.pll scucessfuly