SQL Loader Part 1
SQL Loader Part 1
SQL LOADER is an Oracle utility used to load data into table given a datafile which has the
records that need to be loaded. SQL*Loader takes data file, as well as a control file, to insert data
into the table. When a Control file is executed, it can create Three (3) files called log file, bad file
or reject file, discard file.
Log file tells you the state of the tables and indexes and the number of logical records
already read from the input datafile. This information can be used to resume the load
where it left off.
Bad file or reject file gives you the records that were rejected because of formatting
errors or because they caused Oracle errors.
Discard file specifies the records that do not meet any of the loading criteria like when
any of the WHEN clauses specified in the control file. These records differ from rejected
records.
CLERK
7902 12/17/1980
7499 ALLEN
800
1600
7521 WARD
1250
7566 JONES
2975
7654 MARTIN
1250
7698 BLAKE
2850
7782 CLARK
2450
7788 SCOTT
ANALYST
7839 KING
PRESIDENT
7566 12/9/1982
11/17/1981
3000
5000
7844 TURNER
7876 ADAMS
CLERK
7788 1/12/1983
1100
7900 JAMES
CLERK
7698 12/3/1981
950
7902 FORD
ANALYST
7566 12/3/1981
3000
7782 1/23/1982
1300
7934 MILLER
CLERK
1500
Variable Record Format would like below where the data fields are separated by a delimiter.
Note: The Delimiter can be anything you like. In this case it is |
1196700|9|0|692.64
1378901|2|3900|488.62
1418700|2|2320|467.92
1418702|14|8740|4056.36
1499100|1|0|3.68
1632800|3|0|1866.66
1632900|1|70|12.64
1637600|50|0|755.5
LOAD DATA
INFILE '$FILE'
APPEND
TRUNCATE
QTY
DECIMAL EXTERNAL,
REVENUE
DECIMAL EXTERNAL,
EXT_COST
DECIMAL EXTERNAL TERMINATED BY WHITESPACE
"(TRIM(:EXT_COST))" ,
MONTH
"to_char(LAST_DAY(ADD_MONTHS(SYSDATE,-1)),'DD-MON-YY')" ,
DIVISION_CODE
CONSTANT "AUD"
Getting value from datafile
OPTION statement precedes the LOAD DATA statement. The OPTIONS parameter allows you
to specify runtime arguments in the control file, rather than on the command line. The following
arguments can be specified using the OPTIONS parameter.
SKIP = n Number of logical records to skip (Default 0)
LOAD = n Number of logical records to load (Default all)
ERRORS = n Number of errors to allow (Default 50)
ROWS = n Number of rows in conventional path bind array or between direct path data
saves (Default: Conventional Path 64, Direct path all)
BINDSIZE = n Size of conventional path bind array in bytes (System-dependent default)
SILENT = {FEEDBACK | ERRORS | DISCARDS | ALL} Suppress messages during run
(header, feedback, errors, discards, partitions, all)
DIRECT = {TRUE | FALSE} Use direct path (Default FALSE)
PARALLEL = {TRUE | FALSE} Perform parallel load (Default FALSE)
LOADDATA statement is required at the beginning of the control file.
INFILE: INFILE keyword is used to specify location of the datafile or datafiles.
INFILE* specifies that the data is found in the control file and not in an external file. INFILE
$FILE, can be used to send the filepath and filename as a parameter when registered as a
concurrent program.
INFILE /home/vision/kap/import2.csv specifies the filepath and the filename.
7654,MARTIN,7698,Accounting
Example where file name and path is sent as a parameter when registered as a concurrent
program
LOAD DATA
INFILE $FILE
INTO TABLE kap_emp
FIELDS TERMINATED BY ,
( emp_num, emp_name, department_num, department_name )
TYPE OF LOADING:
INSERT If the table you are loading is empty, INSERT can be used.
APPEND If data already exists in the table, SQL*Loader appends the new rows to it. If data
doesnt already exist, the new rows are simply loaded.
REPLACE All rows in the table are deleted and the new data is loaded
TRUNCATE SQL*Loader uses the SQL TRUNCATE command.
INTOTABLEis required to identify the table to be loaded into. In the above example INTO
TABLE APPS.BUDGET, APPS refers to the Schema and BUDGET is the Table name.
FIELDS TERMINATED BY specifies how the data fields are terminated in the datafile.(If the
file is Comma delimited or Pipe delimited etc)
OPTIONALLY ENCLOSED BY specifies that data fields may also be enclosed by quotation
marks.
TRAILINGNULLCOLS clause tells SQL*Loader to treat any relatively positioned columns that
are not present in the record as null columns.
INFILE sample.dat
INTO TABLE emp
( empno
ename
job
mgr
sal
comm
deptno
Skip columns:
You can skip columns using the FILLER option.
Load Data
TRAILING NULLCOLS
(
name Filler,
Empno ,
sal
)
here the column name will be skipped.
Please continue to read our part-2 of SQL Loader article
SQL LOADER is a very powerful tool that lets you load data from a delimited or position based
data file into Oracle tables. We have received many questions regarding SQL LOADER features
from many users. Here is the brief explanation on the same.
Please note that the basic knowledge of SQL LOADER is required to understand this article.
This article covers the below topics:
1.Load multiple data files into a single table
2.Load a single data file into multiple tables
3.Skip a column while loading using FILLER and Load field in the delimited data file into two
different columns in a table using POSITION
4.Usage of BOUNDFILLER
5.Load the same record twice into a single table
6.Using WHEN to selectively load the records into the table
7.Run SQLLDR from SQL PLUS
8.Default path for Discard, bad and log files
1)Load multiple files into a single table:
SQL LOADER lets you load multiple data files at once into a single table. But all the data files
should be of the same format.
Here is a working example:
Say you have a table named EMP which has the below structure:
Column
emp_num
emp_name
department_num
department_name
Data Type
Number
Varchar2(25)
Number
Varchar2(25)
You are trying to load the below comma delimited data files named eg.dat and eg1.dat:
eg.dat:
7369,SMITH,7902,Accounting
7499,ALLEN,7698,Sales
7521,WARD,7698,Accounting
7566,JONES,7839,Sales
7654,MARTIN,7698,Accounting
eg1.dat:
1234,Tom,2345,Accounting
3456,Berry,8976,Accounting
LOAD DATA
INFILE eg.dat File 1
INFILE eg1.dat File 2
APPEND
Column
emp_num
emp_name
department_num
department_name
Data Type
Number
Varchar2(25)
Number
Varchar2(25)
You are trying to load the below comma delimited data file named eg.dat which has columns
Emp_num and emp_name that need to be loaded into table EMP and columns department_num
and department_name that need to be loaded into table DEPT using a single CTL file here.
eg.dat:
7369,SMITH,7902,Accounting
7499,ALLEN,7698,Sales
7521,WARD,7698,Accounting
7566,JONES,7839,Sales
7654,MARTIN,7698,Accounting
INFILE eg.dat
APPEND
INTO TABLE emp
FIELDS TERMINATED BY ,
( emp_num, emp_name )
INTO TABLE dept
FIELDS TERMINATED BY ,
(department_num, department_name)
You can further use WHEN clause to selectively load the records into the tables which will be
explained later in this article.
3)Skip a column while loading using FILLER and Load field in the delimited data file into
two different columns in a table using POSITION
SQL LOADER lets to skip unwanted fields in the data file by using the FILLER clause. Filler
was introduced in Oracle 8i.
SQL LOADER also lets you load the same field into two different columns of the table.
If the data file is position based, loading the same field into two different columns is pretty
straight forward. You can use Position (start_pos:end_pos) keyword
If the data file is a delimited file and it has a header included in it, then this can be achieved by
referring the field preceded with : eg description (:emp_name).
If the data file is delimited file without a header included in it, Position (start_pos:end_pos) or
(:field) will not work. This can be achieved using POSITION (1) clause which takes you to the
beginning of the record.
Say you have a table named EMP which has the below structure:
Column
emp_num
emp_name
description
department_num
department_name
Data Type
Number
Varchar2(25)
Varchar2(25)
Number
Varchar2(25)
You are trying to load the below comma delimited data file named eg.dat which has 4 fields that
need to be loaded into 5 columns of the table EMP.
eg.dat:
7369,SMITH,7902,Accounting
7499,ALLEN,7698,Sales
7521,WARD,7698,Accounting
7566,JONES,7839,Sales
7654,MARTIN,7698,Accounting
Control File:
LOAD DATA
INFILE eg.dat
APPEND
INTO TABLE emp
FIELDS TERMINATED BY ,
(emp_num,
emp_name,
Data File:
1,15,7369,SMITH
1,15,7499,ALLEN
1,15,7521,WARD
1,18,7566,JONES
1,20,7654,MARTIN
LOAD DATA
INFILE eg.dat
APPEND
INTO TABLE emp
FIELDS TERMINATED BY ,
( emp_num, emp_name, department_num, department_name )
INTO TABLE emp
FIELDS TERMINATED BY ,
(emp_num POSITION(1),emp_name,department_num,department_name)
2,7902,Accounting
1,7499,ALLEN
2,7698,Sales
1,7521,WARD
2,7698,Accounting
1,7566,JONES
2,7839,Sales
1,7654,MARTIN
2,7698,Accounting
3,10
Control File:
LOAD DATA
INFILE eg.dat
APPEND
INTO TABLE emp
WHEN (01) = 1
FIELDS TERMINATED BY ,
( rec_skip filler POSITION(1),emp_num , emp_name )
INTO TABLE dept
WHEN (01) = 2
FIELDS TERMINATED BY ,
(rec_skip filler POSITION(1),department_num,
department_name )
Lets now see how SQL LOADER processes the CTL file:
SQL LOADER loads the records into table EMP only when first position (01) of the record
which happens to be the record type is 1 as directed by command
INTO TABLE emp
WHEN (01) = 1
If condition When (01) = 1 holds true for the current record, then SQL LOADER gets to the
beginning of the record as directed by command POSITION(1) and skips the first field which is
the record type.
It then loads the second field into emp_num and third field into emp_name column in the table
EMP.
SQL LOADER loads the records into table DEPT only when first position (01) of the record
which happens to be the record type is 2 as directed by the commands
INTO TABLE dept
WHEN (01) = 2
If condition When (01) = 2 holds true for the current record, then SQL LOADER gets to the
beginning of the record as directed by command POSITION(1) and skips the first field which is
the record type.
It then loads the second field into department_num and third field into department_name
columns in the table DEPT.
The records with record type = 3 are not loaded into any table.
Thus you can selectively loads the necessary records into various tables using WHEN clause.
If bad and discard file paths are not specified in the CTL file and if this SQL Loader is registered
as a concurrent program, then they will be created in the directory where the regular Concurrent
programs output files reside. You can also find the paths where the discard and bad files have
been created in the log file of the SQL LOADER concurrent request.