You are on page 1of 8

SQL*Loader

SQL*Loader can be used to load data from a file into Oracle tables. It is possible to selectivly
exclude records and transform records while they're loaded. The load produces a log file, bad files
and discard files. The control file specifies the behaviour of the SQL*Loader is started with sqlldr

Control file

The SQL*Loader control file must not be confused with the control file It specifies the behaviour
of SQL*Loader.

Three sections
A control file contains three sections.
 The first section contains global options: bindsize, rows, records to skip etc. Also, the
INFILE clause determines the location of the input data.
 The second section contains one or more INTO TABLE blocks.
 The (optional) third section contains input data.

Control file statements

LOAD DATA
This statement indicates that this is the beginning of a new data load.

INFILE
The INFILE statement specifies the file to be loaded.
It also indicates whether the records are fixed length, variable length or stream format; The
following infile statement indicates that each record to be loaded has 11 bytes. Note, that carriage
returns and line feeds also count as bytes.

Fixed format
infile 'precious_data.txt' "fix 11"

The following infile statement indicates that each record to be loaded is of variable length. A
record starts either at the beginning of the input file or after a carriage return/line feed. The length
of each record is indicated at the beginning of each record and consists of 4 bytes.

Variable format
infile 'precious_data.txt' "var 4"

Stream format
The following infile statement indicates that the input data is in stream format. That is,
SQL*Loader searches for a record seperator when it reads the input data. The record seperator in
this case is either a bar (|) or a line feed.

infile 'precious_data.txt' "str '|\n'"

In stream format, it would also be possible to say \t for tabulator, \f for form feed, \v for vertical
tabulator, \r for carriage return.
INFILE *
If the data to be loaded is located in the control file itself, the file name is a star.
DISCARDFILE
See example 3.
INTO TABLE
The into table statement defines the relationship between the fields in an Oracle table and the
records in the datafile to be loaded.
APPEND
One of the options to be used if the table is non empty. See also INSERT.
INSERT
FIELDS TERMINIATED BY
If the fields of the data that is being loaded are not fixed width, it must be specified how they're
seperated.
See Example 2.
WHEN
When is used to selectively load data (according to a criteria stated after the when) into the table to
be loaded. See also example 3

BEGINDATA
If the data to be loaded is in the control file itself, the start of the data is marked with a
BEGINDATA statement.

Bad file
If a record cannot be loaded because it is rejected (either by Oracle itself or SQL*Loader), it will
be written into the bad file.

Discard file
The discard file contains records that didn't meet any of the record selection critierias.

This example shows how to insert records into an empty table. One column is filled with the
current sysdate. First, the table to be filled is created:

create table sql_loader_1 (


load_time date,
field_1 varchar2(10),
field_2 varchar2(10)
);

Here's the control file. Note that the positions 11 through 20 are loaded into field_1 and positions 1
through 10 into field_2. The field load_time is filled with the current
time (sysdate) of the load.

load_1.ctl
----------

load data
infile 'load_1.dat' "str '\r\n'"
insert
into table sql_loader_1
(
load_time sysdate,
field_2 position( 1:10),
field_1 position(11:20)
)
Here's the data. The name of the file (load_1.dat) had been specified with
the infile statement in the control file.

load_1.dat
0123456789abcdefghij
**********##########
foo bar
here comes a very long line
and the next is
short

sqlldr control=load_1.ctl userid=rene/rene

LOAD_TIME FIELD_1 FIELD_2


------------------- ---------- ----------
23.08.2004 15:42:43 abcdefghij 0123456789
23.08.2004 15:42:43 ########## **********
23.08.2004 15:42:43 bar foo
23.08.2004 15:42:43 a very lo here comes
23.08.2004 15:42:43 xt is and the ne
23.08.2004 15:42:43 short

This example shows how to selectively insert records according to a criteria.


First, the table to be filled is created:

create table sql_loader_3 (


field_1 varchar2(10),
field_2 varchar2(10)
);

Here's the control file.


It specifies that records whose second field equals Fruit is loaded.

load_3.ctl
----------

load data
infile 'load_3.dat' "str '\r\n'"
discardfile 'load_3.dsc'
insert
into table sql_loader_3
when field_2 = 'Fruit'
fields terminated by ';'
(
field_1 char,
field_2 char
)

Here's the data. The name of the file (load_3.dat)


had been specified with the infile statement in the control file.
load_3.dat
----------

Banana;Fruit;
Lemon;Fruit;
Avocado;Fruit
Ford;Car;
Pear;Fruit;
Apple;Fruit;

The following command actually loads the data.


sqlldr control=load_3.ctl userid=rene/rene

The table now contains (select * from sql_loader_3):


FIELD_1 FIELD_2
---------- ----------
Banana Fruit
Lemon Fruit
Avocado Fruit
Pear Fruit
Apple Fruit

One record didn't meet the criteria. It appears in the discard file:

load_3.dsc
----------
Ford;Car;

This example shows how to insert records that are seperated by a comma.
First, the table to be filled is created:

create table sql_loader_2 (


field_1 varchar2(10),
field_2 number
);

Here's the control file. Note, that the second field is specified as integer external.
That means, that the numbers in the dat file are human readable rather than "real" bytes.

load_2.ctl
----------load data
infile 'load_2.dat' "str '\r\n'"
insert
into table sql_loader_2
fields terminated by ','
(
field_1 char,
field_2 integer external
)
Here's the data. The name of the file (load_2.dat) had been specified with the infile statement in the
control file.
load_2.dat
----------

one,1
two,2
fifty,50
eighty-eight,88
one hundred,100
fifteen,15

The following command actually loads the data.


sqlldr control=load_2.ctl userid=rene/rene

The table now contains (select * from sql_loader_2):


FIELD_1 FIELD_2
---------- ----------
one 1
two 2
fifty 50
fifteen 15

Two records could not be loaded, they appear in the bad file:

load_2.bad
-------------
eighty-eight,88
one hundred,100

This example shows how to selectively insert records according to a criteria.


It is similar to example 3 but fills two tables. First, the tables to be filled are created:

create table sql_loader_4_a (


field_1 varchar2(10),
field_2 varchar2(10)
);

create table sql_loader_4_b (


field_1 varchar2(10),
field_2 varchar2(10)
);

load_4.ctl
----------

load data
infile 'load_4.dat' "str '\r\n'"
discardfile 'load_4.dsc'
insert
into table sql_loader_4_a
when field_2 = 'Fruit'
(
field_1 position(1) char(8),
field_2 position(9) char(5)
)
into table sql_loader_4_b
when field_2 = 'City'
(
field_1 position(1) char(8),
field_2 position(9) char(5)
)

Here's the data. The name of the file (load_4.dat)


had been specified with the infile statement in the control file.

load_4.dat
----------

Banana Fruit
Lemon Fruit
Tokyo City
Avocado Fruit
Boston City
Ford Car
Pear Fruit
Apple Fruit

The following command actually loads the data.


sqlldr control=load_4.ctl userid=rene/rene

sql_loader_4_a now contains (select * from sql_loader_4_a):


FIELD_1 FIELD_2
---------- ----------
Banana Fruit
Lemon Fruit
Avocado Fruit
Pear Fruit
Apple Fruit

sql_loader_4_b now contains (select * from sql_loader_4_b):


FIELD_1 FIELD_2
---------- ----------
Tokyo City
Boston City

One record didn't meet the criteria. It appears in the discard file:

load_4.dsc
----------

Ford Car
OPTIONS (READSIZE=200000, BINDSIZE=200000, SILENT=FEEDBACK)
LOAD DATA
APPEND
INTO TABLE PO_HEADERS
WHEN record_type = '100'
FIELDS TERMINATED BY '|'
TRAILING NULLCOLS
(record_type,
PURCHASE_ORDER_NUMBER,
RELEASE_NUMBER,
PO_HEADER_ID,
PO_RELEASE_ID,
PO_FLEXFIELD1,
PO_FLEXFIELD2,
PO_FLEXFIELD3,
PO_FLEXFIELD4,
PO_FLEXFIELD5)

INTO TABLE PO_LINES


WHEN record_type = '200'
FIELDS TERMINATED BY '|'
TRAILING NULLCOLS
(record_type POSITION(1),
LINE_NUMBER,
LINE_ORDER_QUANTITY,
LINE_NOTE_TO_SUPPLIER,
LINE_FLEXFIELD_CONTEXT,
LINE_FLEXFIELD1,
LINE_FLEXFIELD2,
LINE_FLEXFIELD3,
PCARD_BRAND,
PO_HEADER_ID)

INTO TABLE PO_LINE_LOCATIONS


WHEN record_type = '300'
FIELDS TERMINATED BY '|'
TRAILING NULLCOLS
(record_type POSITION(1),
SHIPMENT_NUMBER,
UOM_CODE,
FOB_CODE_INTERNAL,
FREIGHT_TERMS_INTERNAL,
TAXABLE_FLAG,
TAX_CODE,
SHIPMENT_FLEXFIELD_CONTEXT,
SHIPMENT_FLEXFIELD1,
SHIPMENT_FLEXFIELD2,
SHIPMENT_FLEXFIELD3)

INTO TABLE PO_DISTRIBUTIONS


WHEN record_type = '400'
FIELDS TERMINATED BY '|'
TRAILING NULLCOLS
(ACCOUNT_SEGMENT1,
ACCOUNT_SEGMENT2,
ACCOUNT_SEGMENT3,
ACCOUNT_SEGMENT4,
ACCOUNT_SEGMENT5,
ACCOUNT_SEGMENT6,
ACCOUNT_SEGMENT7,
ACCOUNT_SEGMENT8,
ACCOUNT_SEGMENT9)

INTO TABLE GEPS_PO_POPOCHEADER_INF


WHEN record_type = '000'
FIELDS TERMINATED BY '|'
TRAILING NULLCOLS
(
RECORD_TYPE POSITION(1),
BUSINESS_UNIT,
ORG_ID,
INTERFACE_CODE,
BATCH_ID,
FILE_CREATION_DATE,
SOURCE_SYSTEM
)

INTO TABLE PO_POPOCTRAILER


WHEN record_type = '999'
FIELDS TERMINATED BY '|'
TRAILING NULLCOLS
(
RECORD_TYPE POSITION(1),
SOURCE_SYSTEM,
ORG_ID,
NUM_POHEADER_REC,
NUM_POLINES_REC,
NUM_POLINELOC_REC,
NUM_PODIST_REC
)

You might also like