05.11.2015 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 15 ■ DATA LOADING AND UNLOADING 701<br />

Things to note in the .dat file are as follows:<br />

• Each field is enclosed in our enclosure character.<br />

• The DATEs are unloaded as large numbers.<br />

• Each line of data in this file ends with a ~ as requested.<br />

We can now reload this data easily using SQLLDR. You may add options to the SQLLDR<br />

comm<strong>and</strong> line as you see fit.<br />

As stated previously, the logic of the unload package may be implemented in a variety of<br />

languages <strong>and</strong> tools. On the Ask Tom web site, you will find this example implemented not<br />

only in PL/SQL as here, but also in Pro*C <strong>and</strong> SQL*Plus scripts. Pro*C is the fastest implementation,<br />

<strong>and</strong> it always writes to the client workstation file system. PL/SQL is a good all-around<br />

implementation (there’s no need to compile <strong>and</strong> install on client workstations), but it always<br />

writes to the server file system. SQL*Plus is a good middle ground, offering fair performance<br />

<strong>and</strong> the ability to write to the client file system.<br />

Data Pump Unload<br />

<strong>Oracle</strong>9i introduced external tables as a method to read external data into the database.<br />

<strong>Oracle</strong> 10g introduced the ability to go the other direction <strong>and</strong> use a CREATE TABLE statement<br />

to create external data, to unload data from the database. As of <strong>Oracle</strong> 10g, this data is<br />

extracted in a proprietary binary format known as Data Pump format, which is the same<br />

format the EXPDB <strong>and</strong> IMPDP tools provided by <strong>Oracle</strong> use to move data from database to<br />

database.<br />

Using the external table unload is actually quite easy—as easy as a CREATE TABLE AS<br />

SELECT statement. To start, we need a DIRECTORY object:<br />

ops$tkyte@ORA10GR1> create or replace directory tmp as '/tmp'<br />

2 /<br />

Directory created.<br />

Now we are ready to unload data to this directory using a simple SELECT statement, for<br />

example:<br />

ops$tkyte@ORA10GR1> create table all_objects_unload<br />

2 organization external<br />

3 ( type oracle_datapump<br />

4 default directory TMP<br />

5 location( 'allobjects.dat' )<br />

6 )<br />

7 as<br />

8 select<br />

9 *<br />

10 from all_objects<br />

11 /<br />

Table created.

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

Saved successfully!

Ooh no, something went wrong!