Oracle UTL_FILE读取CSV文件行

时间:2019-05-27 16:07:49

标签: oracle plsql

我正在使用UTL_FILE包读取csv文件,然后在表中插入值,但是我的问题是如何读取用逗号分隔的值。。,这是我的代码:

     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; 
        /

这是我的csv文件:

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

请问您对我的问题有什么建议吗?

致谢。

2 个答案:

答案 0 :(得分:1)

下面是一个示例,显示了如何执行此操作。我的代码与您的代码略有不同,因为目录和文件名不同。

样本表,它将包含存储在文件中的数据:

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

Table created.

代码;有趣的是第14行,以及将整行拆分为单独值的方法:

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.

结果:

SQL> select * from test2;

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

SQL>

答案 1 :(得分:1)

或者,您可以使用“ external tables”来读取CVS文件。 因此,您可以将文件“查询”为表。

1-如果您的Oracle数据库处于打开状态:
a-Windows,然后使用“以'\ r \ n'分隔”
b-Linux / unix,然后使用“以'\ n'分隔”

2-文件no的第一行具有列名。

3-分隔符是逗号,ASCII 44,“ ”。

4个字段可以将数据括在引号之间,ASCII34。
因此值可以包含空格,逗号和双引号作为单引号。

  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;