Oracle
  Home arrow Oracle arrow Writing to Text Files in Oracle PL/SQL
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
ORACLE

Writing to Text Files in Oracle PL/SQL
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 89
    2006-04-18


    Table of Contents:
  • Writing to Text Files in Oracle PL/SQL
  • How to count the number of lines from the text file
  • How to copy the information from a text file into a table using PL/SQL
  • How to write into a text file from PL/SQL
  • How to copy from a table into the text file from PL/SQL
  • Other ways to write the information into a text file from PL/SQL

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Writing to Text Files in Oracle PL/SQL
    ( Page 1 of 6 )

    This is the second article in a series focusing on file input/output using Oracle PL/SQL packages.

    If you are new to working with UTL_FILE, I strongly suggest you go through my first article in this series.  It gives you all the necessary steps for creating and developing PL/SQL with UTL_FILE for beginners.

    How to read multiple lines from the text file using PL/SQL

    In the previous article, we have seen how to work with only the first line in the text file. But no one would ever be happy working with only a single line.  In this section, we shall work on more than one line (or read all lines) from the text file.

    We will also need to work with exceptions in this article.  If you are new to exception handling in PL/SQL, I strongly suggest you refer to my contributions at:

    http://www.devshed.com/c/a/Oracle/Database-Interaction-with-PLSQL-
    Predefined-Exceptions/

    http://www.devshed.com/c/a/Oracle/Database-Interaction-with-PLSQL-User-
    defined-Exceptions-Nested-Blocks/

    Now, let us follow the sample code:

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

    Since I explained much of the above program in my previous article, I don’t want to go through every line.  The most important issue from the above program is that I used “utl_file.get_line” in an infinite loop (a loop that would never finish). 

    When does the loop terminate?  It all depends on the “exception.”  When the PL/SQL run-time successfully reads a line from the text file, it simply places that information into the variable “s” (which gets displayed directly onto the screen). 

    When it is unable to read any more lines from the text file, it simply raises an exception (NO_DATA_FOUND), which gets handled in the “exception” section.  Once an exception gets raised, the control will automatically terminate the loop abnormally.



     
     
    >>> More Oracle Articles          >>> More By Jagadish Chatarji
     

       

    ORACLE ARTICLES

    - Oracle's Turn to Play in the Sun
    - Implementing and Using Oracle`s Restore Poin...
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek