0% found this document useful (0 votes)
67 views

27.working With SQL Loader

Sql*loader moves data from external flat files into Oracle databases. It can load data from multiple file types, handle different record formats, manipulate data fields before inserting, and load multiple tables in a single run. The control file specifies how to load the data, including the table, field delimiters, and data. Sqlldr is invoked with the control file to load the data into the database. Examples show loading data into tables from control files and flat files, generating keys, combining records, and loading normalized data across multiple tables.

Uploaded by

niaam
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

27.working With SQL Loader

Sql*loader moves data from external flat files into Oracle databases. It can load data from multiple file types, handle different record formats, manipulate data fields before inserting, and load multiple tables in a single run. The control file specifies how to load the data, including the table, field delimiters, and data. Sqlldr is invoked with the control file to load the data into the database. Examples show loading data into tables from control files and flat files, generating keys, combining records, and loading normalized data across multiple tables.

Uploaded by

niaam
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

27.

Working with SQL * Loader

Sql*loader moves data from external flat files into Oracle databse.

 SQL*Loader can perform the following things:


 Load data from multiple data files of different file types
 Handle fixed-format, delimited-format, and variable –length records
 Manipulate data fields with SQL functions before inserting the data into database columns.
 Supports a wide range of data types, including DATE,BINARY,PACKED DECIMAL and
ZONED DECIMAL
 Load multiple tables during the same run, loading selected rows into each table
 Combines multiple physical records into a single logical record, treats a single physical record as
multiple logical records
 Generate unique, sequential key values in specified columns
 Use your operating system’s file or record management system to access datafiles
 Load data from disk or tape
 Provide thorough error reporting capabilities, so you can easily adjust and load all records.

The control file contains the DDL definitions.

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.

To invoke SQL*Loader the command is,

Syntax:
$ sqllar <OPTIONS>

The options are as follows:


Userid Username and password
Controlfile Control file name
Log Log file name
Bad Bad file name
Data Data file name
Discard Discard file name
Discardmax Number of discards to allow
Skip Number of logical records to skip (Default 0)
Load Number of logical records to load (Default 0)
Errors Number of errors to allow(Default 0)
Rows Number of rows in conventional path
Direct Direct path data saves (Default 64 in conventional,
all in direct(
Bindsize Size of conventional path bind array in bytes
(System-dependent)
Slient Suppress messages during run
Parfile Parameter file

For example:

$ Sqlldr userid=scott/manager control=casel.ct1 log=case1.log


$ sqlldr control=case.ct1 log=case.log data=etc.dat userid=steeve/abc
errors=40 load=2000 discard=abc.dis discardmax=2
Or
$ vi par.file --File with the parameters
control=case.ct1
log=case.log
data=etc.dat
userid=steeve/abc
errors=40
load=2000
discard=abc.dia
discardmax=2
$ sqlldr parfile=par.file

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

Case2:loads the data of case2.dat to the table EMP.

$ 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

$ sqlldr userid=scott/tiger control=case2.ct1 log=case2.log

Case3: adds the data into EMP table using sequence function. Sequence function generates unique keys for
loaded data.

SQL> alter table emp add (projno number, loadseq number)


$ vi case3.ct1
load data infile * append into table emp
fileds terminated by “ , “ optionally enclosed by ‘ “ ‘
(empno, ename, job, mgr, hiredate date (20) “dd-month-yyyy”,
sal, comm., deptno, char teminated by ‘:
projno, loadseq sequence (max, 1) )
begindata
7782, “clark”,”manager”, 7839,09-jun-1981,2572.50, ,10:101
7839, “king” , “president”, ,17-nov-1981,5500.00, ,10:102
7934, “miller”, “clerk”, 7782,23-jan-1982,920.00, ,10:102
7566 “jones”, “manager”, 7839,02-apr-1981,3123.75, ,20:101
7499, “allen”, “salesman”, 7698,20-feb-1981, 1600.00,300.00,30:103
7654, “martin”,”salesman”, 7689,28-sep-1981,1312.50,1400,30:103

$ sqlldr userid=scott /tiger control=case3.ct1 log=case3.log

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

You might also like