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 681<br />

With those three situations in mind, in general I strongly recommend using external<br />

tables for their extended capabilities. SQLLDR is a fairly simple tool that generates an INSERT<br />

statement <strong>and</strong> loads data. Its ability to use SQL is limited to calling SQL functions on a<br />

row-by-row basis. External tables open up the entire SQL set of functionality to data loading.<br />

Some of the key functionality features that external tables have over SQLLDR in my experience<br />

are as follows:<br />

• The ability to use complex WHERE conditions to selectively load data. SQLLDR has a WHEN<br />

clause to select rows to load, but you are limited to using only AND expressions <strong>and</strong><br />

expressions using equality—no ranges (greater than, less than), no OR expressions, no<br />

IS NULL, <strong>and</strong> so on.<br />

• The ability to MERGE data. You can take an operating system file full of data <strong>and</strong> update<br />

existing database records from it.<br />

• The ability to perform efficient code lookups. You can join an external table to other<br />

database tables as part of your load process.<br />

• Easier multitable inserts using INSERT. Starting in <strong>Oracle</strong>9i, an INSERT statement can<br />

insert into one or more tables using complex WHEN conditions. While SQLLDR can load<br />

into multiple tables, it can be quite complex to formulate the syntax.<br />

• A shallower learning curve for new developers. SQLLDR is “yet another tool” to learn, in<br />

addition to the programming language, the development tools, the SQL language, <strong>and</strong><br />

so on. As long as a developer knows SQL, he can immediately apply that knowledge to<br />

bulk data loading, without having to learn a new tool (SQLLDR).<br />

So, with that in mind, let’s look at how to use external tables.<br />

Setting Up External Tables<br />

As a first simple demonstration of external tables, we’ll rerun the previous SQLLDR example,<br />

which bulk loaded data into the DEPT table. Just to refresh your memory, the simple control file<br />

we used was as follows:<br />

LOAD DATA<br />

INFILE *<br />

INTO TABLE DEPT<br />

FIELDS TERMINATED BY ','<br />

(DEPTNO, DNAME, LOC )<br />

BEGINDATA<br />

10,Sales,Virginia<br />

20,Accounting,Virginia<br />

30,Consulting,Virginia<br />

40,Finance,Virginia<br />

By far the easiest way to get started is to use this existing legacy control file to provide the<br />

definition of our external table. The following SQLLDR comm<strong>and</strong> will generate the CREATE<br />

TABLE statement for our external table:

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

Saved successfully!

Ooh no, something went wrong!