Home arrow Oracle arrow Page 2 - Writing to Text Files in Oracle PL/SQL

How to count the number of lines from the text file using PL/SQL - Oracle

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

TABLE OF CONTENTS:
  1. Writing to Text Files in Oracle PL/SQL
  2. How to count the number of lines from the text file
  3. How to copy the information from a text file into a table using PL/SQL
  4. How to write into a text file from PL/SQL
  5. How to copy from a table into the text file from PL/SQL
  6. Other ways to write the information into a text file from PL/SQL
By: Jagadish Chatarji
Rating: starstarstarstarstar / 139
April 18, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the previous section, you learned how to display all the lines available in a text file.  Now, let us see how to count the number of lines in a text file.  I modified the program in the previous section to meet the new requirement.  The program is as follows: 

declare
    f utl_file.file_type;
    s varchar2(200);
    c number := 0;
begin
    f := utl_file.fopen('SAMPLEDATA','sample1.txt','R');
    loop
        utl_file.get_line(f,s);
        dbms_output.put_line(s);
        c := c + 1;
    end loop;
exception
    when NO_DATA_FOUND then
        utl_file.fclose(f);
        dbms_output.put_line('Number of lines: ' || c);
end;

According to the above program, I am defining a simple variable to maintain the count.  That variable always gets incremented within the loop, thus maintaining the number of lines read!

You can also observe that I am handling the last part of my program only in the “exception” area.  The exception part gets executed only when no lines are available to read.  In my upcoming articles, I shall explain how to deal with these types of scenarios very professionally.  Till then, I shall continue in this fashion.



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

blog comments powered by Disqus
   

ORACLE ARTICLES

- Oracle Releases Communications Network Integ...
- Oracle Releases Communications Data Model 11...
- Oracle Releases PeopleSoft PeopleTools 8.52
- Oracle Integrates Cloudera Apache Distro, My...
- Oracle Releases MySQL 5.5.18
- Oracle Announces NoSQL Database Availability
- Sorting Database Columns With the SELECT Sta...
- Retrieving Table Data with the LIKE Operator
- Using the IN and BETWEEN Operators on Tables
- Clauses and Logical Operators for Retrieving...
- Limiting Rows When Retrieving Table Data
- Using Scalar Functions for Retrieving Data
- Retrieving Data with String and Arithmatic E...
- Coding the SELECT Statement
- Oracle Releases iPad Virtual Desktop and Exa...


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap

Dev Shed Tutorial Topics: