Oracle UTL_FILE read CSV file lines

霸气de小男生 提交于 2021-02-04 14:18:14

问题


i'm using UTL_FILE package to read a csv file , then insert values in table, but my issue is how to read values separated by Commas.. , this is my code :

     declare
         file1 UTL_FILE.FILE_TYPE;
         str varchar2(200 CHAR);
        begin
         file1 := UTL_FILE.FOPEN('DRCT1','test_file.csv','R');

         loop
          UTL_FILE.GET_LINE(file1,str);
-- here i want to read each value before the Commas then insert them in my table
-- insert statement..
          dbms_output.put_line(str);
         end loop;

        exception
        when no_data_found then
        UTL_FILE.FCLOSE(file1);

        end; 
        /

this is my csv file :

100,Steven,King
101,Neena,Kochha
102,Lex,De Haan
103,Alexander
104,Bruce,Ernst

please do you have any suggestion to my issue ?

Regards .


回答1:


Here's an example which shows how to do that. My code slightly differs from yours because of different directory and file names.

Sample table, which will contain data stored in the file:

SQL> create table test2 (id number, fname varchar2(20), lname varchar2(20));

Table created.

Code; interesting part is line 14 and the way to split the whole row into separate values:

SQL> declare
  2    l_file         utl_file.file_type;
  3    l_text         varchar2(32767);
  4    l_cnt          number;
  5  begin
  6    -- Open file.
  7    l_file := utl_file.fopen('EXT_DIR', 'test2.txt', 'R', 32767);
  8
  9    loop
 10      utl_file.get_line(l_file, l_text, 32767);
 11
 12      -- L_TEXT contains the whole row; split it (by commas) into 3 values
 13      -- and insert them into the TEST2 table
 14      insert into test2 (id, fname, lname)
 15        values (regexp_substr(l_text, '[^,]+', 1, 1),
 16                regexp_substr(l_text, '[^,]+', 1, 2),
 17                regexp_substr(l_text, '[^,]+', 1, 3)
 18               );
 19    end loop;
 20
 21    utl_file.fclose(l_file);
 22  exception
 23    when no_data_found then
 24      null;
 25  end;
 26  /

PL/SQL procedure successfully completed.

The result:

SQL> select * from test2;

        ID FNAME                LNAME
---------- -------------------- --------------------
       100 Steven               King
       101 Neena                Kochha
       102 Lex                  De Haan
       103 Alexander
       104 Bruce                Ernst

SQL>



回答2:


As an alternative you can use "external tables" to read the CVS file. So you can "query" the file as a table.

1-If your Oracle database is on:
a-Windows, then use "delimited by '\r\n'"
b-Linux/unix, then use "delimited by '\n'"

2-First row of file no have column names.

3-Delimiter is comma, ascii 44, ",".

4-Fields can have data enclosed between quotes, ascii 34.
So values can have spaces, comma, and double quotes as one quote.

  create table test_file_ext
  (id      number,
   fname   varchar2(200),
   lname   varchar2(200)
  )
  organization external
  (type oracle_loader
   default directory DRCT1
   access parameters (records      delimited by '\r\n'
                      badfile     'test_file_ext.bad'
                      discardfile 'test_file_ext.dis'
                      logfile     'test_file_ext.log'
                      fields terminated by ','
                             optionally enclosed by '"'
                             missing field values are null
                             reject rows with all null fields 
                     )
   location ('test_file.csv')
  )
  reject limit UNLIMITED;


来源:https://stackoverflow.com/questions/56329368/oracle-utl-file-read-csv-file-lines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!