Oracle
  Home arrow Oracle arrow Page 2 - Reading Text Files using Oracle PL/SQL...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ORACLE

Reading Text Files using Oracle PL/SQL and UTL_FILE
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 63
    2006-04-11

    Table of Contents:
  • Reading Text Files using Oracle PL/SQL and UTL_FILE
  • How to read a text file from a file system and display the contents using PL/SQL
  • How to read a text file from a file system and display the contents using PL/SQL: discussion
  • Improving it a bit with exception handling

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Reading Text Files using Oracle PL/SQL and UTL_FILE - How to read a text file from a file system and display the contents using PL/SQL
    (Page 2 of 4 )

    In this section, we shall look at reading a text file from a host operating system.  Please understand that I am giving examples based on Windows only.  The paths would be a bit different if you use any non-Windows environment.

    The first step we need to perform is nothing but letting the Oracle database know on which directory we are working (reading or writing files only from that directory).  This can be achieved using the following command:

    CREATE DIRECTORY sampledata AS 'c:sampledata';

    The above command creates a new directory entry into the Oracle database with a logical name to the directory, "SAMPLEDATA" (pointing to the path "c:sampledata").  To execute the above command, you need to have certain privileges.  I suggest you log in using "SYSTEM" user (or contact your database administrator on this issue).  If you wanted to list all the directories registered, you can use the following command:

    SELECT * FROM ALL_DIRECTORIES;

    The second step we need to perform is providing necessary permissions to the user to work with that directory.  This can be achieved with something like the following:

    GRANT read, write ON DIRECTORY sampledata TO PUBLIC;

    The above step makes sure that the directory "sampledata" (or "c:sampledata") is accessible to every user (PUBLIC).  It is not at all a good idea to provide grants to the PUBLIC.  I suggest you provide the grants only to the required users (who really need them).

    The third step we need to perform is simply creating a new text file within the folder "c:sampledata".  I hope you can do it yourself.

    The last and final step is trying to read the file using PL/SQL.  Open your SQL*PLUS environment and copy the following code:

    declare
    f utl_file.file_type;
    s varchar2(200);
    begin
    f := utl_file.fopen('SAMPLEDATA','sample1.txt','R');
    utl_file.get_line(f,s);
    utl_file.fclose(f);
    dbms_output.put_line(s);
    end;
    /

    Before executing the above program, make sure that you execute the statement "SET SERVEROUTPUT ON" (so that the result gets displayed on the screen).  Once you complete all of the above steps, it should run successfully, retrieving the first line of the text file. 

    The next section gives you the explanation of the previous PL/SQL program in detail.

    More Oracle Articles
    More By Jagadish Chatarji


       · Hello guys. Starting a small series to work with text files using PL/SQL. Enjoy...
     

       

    ORACLE ARTICLES

    - Tuning PL/SQL Code
    - Debugging PL/SQL Code
    - Testing PL/SQL Code
    - Working With PL/SQL Code
    - Conditional Compilation for Oracle Database ...
    - Compile-Time Warnings for Oracle DB 10g
    - Compiling PL/SQL Code for an Oracle Database
    - Troubleshooting PL/SQL Code
    - Managing PL/SQL Code
    - Data Manipulation and More for HTML DB Appli...
    - Oracle Database Fundamentals
    - Adding Processes to HTML DB Applications
    - Adding Computations, Processes, and Validati...
    - Sub-templates and More with Oracle HTML DB
    - Focusing on Templates in Oracle HTML DB

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway