05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

692<br />

CHAPTER 15 ■ DATA LOADING AND UNLOADING<br />

However, you still might have issues over lengthy periods of time. The PIDs will be reused<br />

on most operating systems. So the techniques outlined in dealing with errors may well still be<br />

relevant—you’ll need to reset your bad file or rename it if it exists <strong>and</strong> if you determine this to<br />

be an issue.<br />

External Tables Summary<br />

In this section, we explored external tables. They are a new feature of <strong>Oracle</strong>9i <strong>and</strong> later that<br />

may, for the most part, replace SQLLDR. We investigated the quickest way to get going with<br />

external tables: the technique of using SQLLDR to convert the control files we have from past<br />

experiences. We demonstrated some techniques for detecting <strong>and</strong> h<strong>and</strong>ling errors via the bad<br />

files <strong>and</strong>, lastly, we explored some multiuser issues regarding external tables.<br />

We are now ready to get into the last sections in this chapter, which deal with unloading<br />

data from the database.<br />

Flat File Unload<br />

One thing SQLLDR does not do, <strong>and</strong> that <strong>Oracle</strong> supplies no comm<strong>and</strong>-line tools for, is unload<br />

data in a format underst<strong>and</strong>able by SQLLDR or other programs. This would be useful for moving<br />

data from system to system without using EXP/IMP or EXPDP/IMPDP (the new Data Pump<br />

replacements for EXP <strong>and</strong> IMP). Using EXP(DP)/IMP(DP) to move data from system to system<br />

works fine—as long as both systems are <strong>Oracle</strong>.<br />

■Note HTML DB provides a data export feature as part of its SQL Workshop. You may export the information<br />

in a CSV format easily. This works well for a few megabytes of information, but it is not appropriate for<br />

many tens of megabytes or more.<br />

We will develop a small PL/SQL utility that may be used to unload data on a server in a<br />

SQLLDR-friendly format. Also, equivalent tools for doing so in Pro*C <strong>and</strong> SQL*Plus are provided<br />

on the Ask Tom web site at http://asktom.oracle.com/~tkyte/flat/index.html. The PL/SQL<br />

utility will work fine in most small cases, but better performance will be had using Pro*C.<br />

Pro*C <strong>and</strong> SQL*Plus are also useful if you need the files to be generated on the client <strong>and</strong> not<br />

on the server, which is where PL/SQL will create them.<br />

The specification of the package we will create is as follows:<br />

ops$tkyte@ORA10G> create or replace package unloader<br />

2 AUTHID CURRENT_USER<br />

3 as<br />

4 /* Function run -- unloads data from any query into a file<br />

5 <strong>and</strong> creates a control file to reload that<br />

6 data into another table<br />

7<br />

8 p_query = SQL query to "unload". May be virtually any query.<br />

9 p_tname = Table to load into. Will be put into control file.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!