How Can I unload data to a flat file

Many times we are asked


Well here is a way to do it into Excel or any spreadsheet that understands the industry standard SYLK file format: http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:728625409049
Here is a pro*c program that does it to a flat file very fast: http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:459020243348
and here is a PLSQL routine that uses utl_file to do the same: http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:68212348056
A reader (Andy Rivenes) offers this more "robust" plsql implementation based on the original code
And lastly, SQLPlus can do this quite easily but it is a pain to have to write a script/table.  What I've done is setup scripts for UNIX and NT that allow you from the command line to execute things like:
 
$ sqlldr_exp scott/tiger dept
LOAD DATA
INFILE *
INTO TABLE dept
REPLACE
FIELDS TERMINATED BY '|'
(
deptno
,dname
,loc
)
BEGINDATA
10|ACCOUNTING|NEW YORK
20|RESEARCH|DALLAS
30|SALES|RESTON
40|OPERATIONS|BOSTON
As you can see, this script unloaded the scott.dept table into a format that sqlldr can easily reload.  All you would need to do is execute:
 
$ sqlldr_exp scott/tiger dept > dept.ctl
to create a control file that can be moved somewhere else and reloaded back into a dept table very quickly.

I also use a slight modification of this script called "flat".  Flat does the same thing as sqlldr_exp does mostly except that it dumps the data into a tab delimited format without the sqlldr extras at the top.  This makes the extract usable in spreadsheets and such.

In both cases some things you need to be aware of are with regards to this script:

You can download the UNIX and NT scripts here.  They are in a a file "unloader.zip" which can be opened with WinZip 6.0 or up on windows.

Alternately, you can get a gzipped tar file for use on Unix without zip.

All information and materials provided here are provided "as-is"; Oracle disclaims all express and implied warranties, including, the implied warranties of merchantability or fitness for a particular use. Oracle shall not be liable for any damages, including, direct, indirect, incidental, special or consequential damages for loss of profits, revenue, data or data use, incurred by you or any third party in connection with the use of this information or these materials.