Professional Documents
Culture Documents
SQL Loader Practice Session
SQL Loader Practice Session
A simple control file identifying one table and three columns to be loaded.
Including the data to be loaded in the control file itself, so there is no separate
data file.
Control File
1) LOAD DATA
2) INFILE *
3) INTO TABLE dept_ldr_trg
4) FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
5) (deptno, dname, loc)
6) BEGINDATA
123,SALES,"DELHI"
124,"ACCOUNTING",HYDERABAD
125,"PACKING",KOLKATTA
126,FINANCE,"MUMBAI"
127,"HR",CHENNAI.
128,"ADMIN",PUNE
129,"RECRUITMENT","GURGAON"
,TRANSPORT,"NOIDA"
Points to note:
1. The LOAD DATA statement marks the beginning of the control file.
2. INFILE * specifies that data is found in the control file and not in an external file.
3. The INTO TABLE statement is required to identify the table to be loaded. Since
no option is mentioned, the default option is INSERT. By default SQL* Loader
requires the table to be empty before it inserts any records.
4. TERMINATED BY ',' specifies that the data is terminated by commas.
OPTIONALLY ENCLOSED BY specifies that the data may be optionally enclosed
by Quotations.
5. The names of the columns to be loaded are enclosed in parentheses. Since no data
type is specified, the default is a character of length 255.
6. BEGINDATA specifies the beginning of the data.
SQL Loader loads the dept_ldr_trg table and creates a log file example1.log.
The last record gets rejected and the same can be checked from the bad file and the log
file as the table dept_trg_ldr has a NOT NULL constraint on the deptno column.
Control File
1) LOAD DATA
2) INFILE 'example2.dat'
3) INTO TABLE emp_ldr_trg
4) (empno POSITION(01:04) INTEGER EXTERNAL,
5) ename POSITION(06:15) CHAR "UPPER(:ename)",
job POSITION(17:25) CHAR,
mgr POSITION(27:30) INTEGER EXTERNAL,
6) hiredate POSITION(63:73) DATE(11)"DD-MON-YYYY",
sal POSITION(32:39) DECIMAL EXTERNAL,
7) comm POSITION(41:48) DECIMAL EXTERNAL "NVL(:comm,0)",
8) deptno POSITION(50:51) CHAR TERMINATED BY ':',
dname POSITION(53:62) CHAR,
9) region CONSTANT '31',
10) time_loaded "TO_CHAR(SYSDATE,'HH24:MI:SS')",
11) date_loaded "TO_DATE(SYSDATE,'DD-MON-YYYY')",
12) load_seq "test_ldr_seq.NEXTVAL",
13) annual_comm "DECODE((:job),'SALESMAN',(:comm*12),100)"
14) )
Points to note:
Data File
Below are a few sample data lines from the file example2.dat.
Control File
LOAD DATA
INFILE 'example4.dat'
DISCARDFILE 'example4.dsc'
DISCARDMAX 999
REPLACE
CONTINUEIF THIS (1) = '*'
INTO TABLE emp_ldr_trg
(empno POSITION(01:04) INTEGER EXTERNAL,
ename POSITION(06:15) CHAR "UPPER(:ename)",
job POSITION(17:25) CHAR,
mgr POSITION(27:30) INTEGER EXTERNAL,
hiredate POSITION(63:73) DATE(11)"DD-MON-YYYY",
sal POSITION(32:39) DECIMAL EXTERNAL,
comm POSITION(41:48) DECIMAL EXTERNAL "NVL(:comm,0)",
deptno POSITION(50:51) CHAR TERMINATED BY ':',
dname POSITION(53:62) CHAR,
region CONSTANT '31',
time_loaded "TO_CHAR(SYSDATE,'HH24:MI:SS')",
date_loaded "TO_DATE(SYSDATE,'DD-MON-YYYY')",
load_seq "test_ldr_seq.NEXTVAL",
annual_comm "DECODE((:job),'SALESMAN',(:comm*12),100)"
)
Points to note:
Data File
Below are a few sample data lines from the file example3.dat.
*1111 CLARK
MANAGER 2111 6543.50 10:MARKETING 15-MAR-1989
*1112 KING
PRESIDENT 9832.00 40 07-JUN-1976
*1113 MILLER
CLERK 2112 720.00 10:MARKETING 25-JAN-1993
*1114 JONES
MANAGER 2113 5432.75 20:ADMIN 10-AUG-1989
*1115 ALLEN
SALESMAN 2114 1900.00 300.00 30:SALES 05-MAR-1990
* MARTIN
SALESMAN 2114 1385.50 1400.00 30:SALES 22-MAR-1997
All the records except for the last one get loaded into the table emp_ldr_trg.
MARTIN’s record gets rejected since we have a not null constraint on the empno column
of the table and the empno in this record is null.
Though a discard file name is mentioned, a discard file does not get created since there
are no records which are rejected due to conditions not satisfied in the control file.
Control File
LOAD DATA
INFILE 'example4.dat'
BADFILE 'example4.bad'
DISCARDFILE 'example4.dsc'
APPEND
INTO TABLE emp_ldr_trg
WHEN deptno != ' '
(empno POSITION(01:04) INTEGER EXTERNAL,
ename POSITION(06:15) CHAR "UPPER(:ename)",
job POSITION(17:25) CHAR,
mgr POSITION(27:30) INTEGER EXTERNAL,
hiredate POSITION(63:73) DATE(11)"DD-MON-YYYY",
sal POSITION(32:39) DECIMAL EXTERNAL,
comm POSITION(41:48) DECIMAL EXTERNAL "NVL(:comm,0)",
deptno POSITION(50:51) CHAR TERMINATED BY ':',
dname POSITION(53:62) CHAR,
region CONSTANT '31',
time_loaded "TO_CHAR(SYSDATE,'HH24:MI:SS')",
date_loaded "TO_DATE(SYSDATE,'DD-MON-YYYY')",
load_seq "test_ldr_seq.NEXTVAL",
annual_comm "DECODE((:job),'SALESMAN',(:comm*12),100)"
)
INTO TABLE dept_ldr_trg
WHEN deptno != ' '
(deptno POSITION(50:51) CHAR TERMINATED BY ':',
dname POSITION(53:62) CHAR)
Points to note
1. The WHEN clause signifies that the records should get loaded only if the DEPTNO
is not null in the data file.
2. The records with DEPTNO as null are rejected and a discard file gets created with
those records.
3. EMP_LDR_TRG and the DEPT_LDR_TRG tables get loaded with the
corresponding data.
4. Use of SKIP option to indicate the number of records to skip.
5. Using APPEND option to append the records to the table without deleting any
existing records.
Data File
Below are a few sample data lines from the file example4.dat.
The discard file gets created with the records which are discarded. In this case with
MONY’s record.