How to read a .CSV file using UTL_FILE

Posted on 2009-02-23
Last Modified: 2012-05-06
How to read a .CSV file using UTL_FILE
Question by:kingbinod
    LVL 12

    Assisted Solution

    Hi there;

    I don't know if the following forum helps but let me give the link:

    Best regards..
    LVL 47

    Assisted Solution

    The best way is to use External Tables that are readonly, i.e. can be used only in SELECT statements
    what ia more then enough.

    create directory med as 'c:\med' ; 
    GRANT READ  ON DIRECTORY med TO lsmedical;
    GRANT WRITE ON DIRECTORY med TO lsmedical;
    drop table lsmedical.USERMEDBASIC_EXT;
    create table lsmedical.USERMEDBASIC_EXT ( 
                       PZN  VARCHAR2(33),
                       NAME VARCHAR2(111)   )
        organization external  
        (type oracle_loader  
         default directory med 
         access parameters (records delimited by newline 
         fields terminated by ',') 
         location ('med-de-formatted.txt','med-de-insulin-formatted.txt',

    Open in new window

    LVL 34

    Accepted Solution

    Here's a sample PL\SQL procedures that I wrote a number of years ago that just shows an example of using UTL_FILE for both reading and writing.  It is not specific to *.CSV files, but it may help:

    create or replace procedure sample (dir_in in varchar2, file_nm in varchar2,
     lines_to_read in number default 3, chars_per_record in number default 999,
     dir_out in varchar2 default 'C:\temp') as
    -- This procedure opens a text file in any directory, reads three lines from it
    --  and writes them to C:\temp\temp.txt.
    -- This is intended for large text files, to quickly get a small section of the file that can
    --  be opened and browsed more easily in a text editor.
      in_file   utl_file.file_type;
      out_file  utl_file.file_type;
      text_str  varchar2(4000);
      lines     pls_integer;
      err_text  varchar2(200);
      lines := 0;
      out_file := utl_file.fopen(dir_out,'temp.txt','W');
      in_file := utl_file.fopen(dir_in,file_nm,'R',chars_per_record);
      while lines < lines_to_read loop
          utl_file.get_line(in_file, text_str);
          lines := lines +1;
          when others then
            err_text := substr(sqlerrm,1,200);
            lines := lines +1;
      end loop;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
    Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
    This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
    This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now