27.working With SQL Loader
27.working With SQL Loader
Sql*loader moves data from external flat files into Oracle databse.
Example:
$ vi casel.ct1
LOAD DATA
INFILE *
INTO TABLE dept
FIELDS TERMINATED BY ‘ , ‘ OPTIONALLY ENCLOSED BY’
(deptno, dname, loc)
BEGINDATA
12, “RESEARCH” , “SARATOGA”
10, “ACCOUNTING” , “CLEVELAND”
Wq
The control file starts with LOAD DATA statement. INFILE specifies that the data is found in the control
file and not in an external file INTO TABLE is to that the table in which the data can be loaded into (dept).
By default SQL*Loader requires the table to be empty before it inserts any records.
Syntax:
$ sqllar <OPTIONS>
For example:
Examples:
Case1: loads the data from the controlfile into the table dept.
$ vi case1.ct1
Load data infile * into table dept
fields terminated by ‘ , ‘ optionally enclosed by ‘ “ ‘
(deptno, dname, loc)
begindata
12, RESEARCH, “SARATOGA”
10, “ACCOUNTING”, CLEVELAND
11, “ART”,SALEM
13,FINANCE, “BOSTON”
21,”SALES” ,PHILA
22,”SALES” ROCHESTER
42,”INT” L”,”SAN FRAN”
:wq
$ sqlldr userid=scott /tiger control=case1.ct1 log=case1.log
$ vi case2.ct1
load data infile ‘case2.dat’ INSERT into table emp
(empno position (01:04) integer external,
ename position (06:15) char,
job position (17:25) char,
mgr position (27:30) integer external,
sal position (32:39) decimal external,
comn position (41:48) decimal external,
deptno position (50:51) integer external)
:wq
$ vi case2.dat
7782 clark Manager7839 2572.50 10
7839 king President 5500 10
7934 Miller clerk 7782 920.00 20
7566 Jones Manager7839 1600.00 300.00 30
7654 Martin Salesman 7698 1312.50 1400.00 30
Case3: adds the data into EMP table using sequence function. Sequence function generates unique keys for
loaded data.
Case4: combines multiple records into one logical record using CONTINUEIF inserting negative
numbers, discardmax is used specify a maximum number of discard and also rejecting records due to
duplicate values in a unique index or or due to invalid data.
$ vi case4.ct1
load data
infile ‘case4.dat’
discardfile ‘case4.dsc’
discardmax 999
replace
continueif this (1) = ‘*’
into table emp
(empno position (1:4) integer external,
ename position (6:15) char,
job position (17:25) char,
mgr position (27:30) integer external,
sal position (32:39) decimal external,
comm. Position (41:48) decimal external,
deptno position (50:51) integer external,
hiredate position (52:60) integer external)
:wq
$ vi case4.dat
*7782 clark manager 7839 2572.50 -10 2512-Nov-85
*7839 king president 5500.00 2505-Apr-83
*7934 miller manager 7839 3123.75 2517-Jul-85
:wq
$ sqlldr userid=scott/tiger control=case4.ct1 log=case4.log
Case 5: explais how to use sqlldr to break down repeating groups in a flat file and load the data into
normalized tables, one record may generate multiple database rows, and use of when clause and also
loading the same field (empno) into multiple tables.
$ vi case5.ct1
load data
infile ‘case5.dat’
badfile ‘case5.bad’
discardfile ‘case5.dsc’
replace
into table emp
(empno position (1:4) integer external,
ename position (6:15) char,
deptno position (17:18) char,
mgr position (20:23) integer external)
into table proj
when projno != ‘ ‘
(empo position (1:4) integer external,
projno position (25:27) integer external)
Into table proj
when projno != ‘ ‘
(empno position (1:4) integer external,
projno position (29:31) integer external)
:wq
$ vi case5.dat
1234 baker 10 9999 101 102 103
1234 joker 10 9999 102 103 104
2664 young 20 2893 101 103 104
:wq
$ sqlldr userid=scoot/tiger control=case5.ct1 log=case5.log
Case6: loads the data into table EMP using the direct path load method and also builds the indexes.
$ vi case6.ct1
load data
infile ‘case6.dat’
insert
into table emp
sorted indexes (empid)
(empno position (1:4) integer external nullif empno=blanks,
ename position (6:15) char,
job position (17:25) char,
mgr position (27:30) integer external nullif mgr=blanks
sal position (32:39) decimal external nullif sal=blanks,
comm. Position (41:48) decimal external nullif comm.=blanks,
deptno position (50:51) integer external nullif depno=blanks)
:wq
$ sqlldr userid=scott/tiger case6.ct1 log=case6.log direct=true