Solved

Oracle PLSQL: Write to output file

Posted on 2004-03-22
1
2,307 Views
Last Modified: 2010-10-05
Hello Everyone,

I'm new to PLSQL and I been testing some of its features.  I need to select columns from a table and write them to an output file.  How can I do this?  For example, let say that I have an "EMP" table that has "NAME", and "Address" columns.  How can I select both of these columns and write it to a file using PL-SQL?

(I've been trying to use a package called "logfilepkg.writelogline" which always seems to give me 21/13    PLS-00201: identifier 'LOGFILEPKG.WRITELOGLINE' must be declared
21/13    PL/SQL: Statement ignored.)

Can anyone provide me with a simple example just to get me started?  Thank you very much for your time.
ITOrBust
0
Comment
Question by:itorbust
1 Comment
 
LVL 34

Accepted Solution

by:
Mark Geerlings earned 120 total points
ID: 10651051
PL\SQL can be used to write output to a text file.  That is not the simplest way to get Oracle data to a text file though.   The simplest way is via SQL*Plus in three steps:
1. spool [a valid path/file_name]
2. select ...
3. spool off

If you want to use PL\SQL, the package that has the procedures you need is utl_file.  Here is an example of that approach:
create or replace procedure sample (path_nm in varchar2, file_nm in varchar2,
 lines_to_read in number default 3, chars_per_record in number default 999,
 output_path 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);
begin
  lines := 0;
  out_file := utl_file.fopen(output_path,'temp.txt','W');
  in_file := utl_file.fopen(path_nm,file_nm,'R',chars_per_record);
  while lines < lines_to_read loop
    begin
      utl_file.get_line(in_file, text_str);
      utl_file.put_line(out_file,nvl(text_str,to_char(lines)));
      lines := lines +1;
    exception
      when others then
        err_text := substr(sqlerrm,1,200);
        lines := lines +1;
        utl_file.put_line(out_file,err_text);
    end;
  end loop;
  utl_file.fclose(in_file);
  utl_file.fclose(out_file);
end;
/
0

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.

Join & Write a Comment

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

708 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

21 Experts available now in Live!

Get 1:1 Help Now