SQL Loader
SQL Loader
I N D I A | U A E | O M A N | Q ATA R | U S A
SQL Loader
4 i a p p s . co m
SQL*Loader – An Introduction
SQL* Loader is an Oracle-supplied utility that allows you to load data from a flat file into one or more database
tables
It can load data from multiple data files during the same load session as well as can load data into multiple tables
during the same load session
SQL*Loader reads data from one or more files specified in the control file
From SQL*Loader’s perspective, the data in the data file is organized as records
2 4iapps.com
Capabilities
SQL*Loader can read from multiple input files in a single load session
SQL*Loader can handle files with fixed-length records, variable-length records, and stream-oriented data
SQL*Loader supports a number of different data types, including text, numeric, decimals and various machine-
specific binary types
SQL*Loader allows you to use Oracle’s built-in SQL functions to manipulate the data being read from the input
file
SQL*Loader includes functionality for dealing with whitespace, delimiters, and null data
3 4iapps.com
SQL*Loader
Control
Input
Data file
File
Data files
Bad
Bad
files
files
4 4iapps.com
Execution Steps
5 4iapps.com
SQL*Loader control file
The control file is a text file written in such a way that SQL*Loader understands. SQL*Loader is the key to any
load process.
It is used to control the following aspects of a SQL*Loader session:
The control file tells SQL*Loader where to find the data, how to parse and interpret the data, where to insert the
data and more
The correspondence between the fields in the input record and the columns in the database tables being loaded
Selection criteria defining which records from the input file contain data to be inserted into the destination
database tables
It also contains the names and locations of the bad file and the discard file
6 4iapps.com
Sample control file
In the example given, control file contains the actual data to be loaded
7 4iapps.com
Control file
In general, the control file has three main sections, in the following order:
A] Session-wide information:
The session-wide information contains the names of the input/output files for the data load
session. Apart from this, other SQL*Loader parameters can also be listed in this section.
LOAD DATA statement is required at the beginning of the control file.
INFILE * specifies that the data is found in the control file and not in an external data file.
BADFILE ‘example1.bad’ indicates that all erroneous records must be stored in the file
example1.bad.
DISCARDFILE ‘example1.dsc’ indicates that all discarded records must be stored in the file
example1.dsc.
8 4iapps.com
Control file continued..
9 4iapps.com
Input Datafiles – Fixed Length format
Input Datafiles:
The data to be loaded is contained in one or more data files if it is not contained in the control file.
The data in the datafile can be in the fixed length format, variable length format, or in the stream record format
A] Fixed Length Format:
A file is in the fixed record format when all the records in the datafile have the same byte length. This format is not
flexible, but offers very good performance. Then, the syntax for the INFILE command is – INFILE student.dat “fix 15″
The syntax for letting SQL*Loader know that the data is in the fixed length format is:
INFILE datafile_name “fix n”
Here INFILE datafile_name refers to the file that contains the data to be loaded. “fix n” implies that each record in the
datafile has a fixed byte length of n.
For example if the name of the following datafile is student.dat and the byte length of a record is 15 bytes
0001, —–Rina, 0002, —-Harry, 0003,—–Sudha
10 4iapps.com
Variable Length format
A file is in the variable record format when the length of each record varies. The length of each record is included at the
beginning of the record in the datafile. This format provides some added flexibility over the fixed record format and a
performance advantage over the stream record format.
For example, you can specify a datafile that is to be interpreted as being in variable record format as follows:
INFILE “datafile_name” “var n”
Here n specifies the number of bytes in the record length field. If n is not specified, SQL*Loader assumes a length of 5
bytes. If n is specified larger than 40 it results in an error. The following datafile is random.dat and the value for n is 3.
009hello,cd,010world,im,
012my,name is,
SQL*Loader reads the first 3 bytes to gather the length of the record. Here the first record is 9 bytes long. After SQL*Loader
has read 9 bytes, it reads the next 3 bytes to find the size of this record which is 10 bytes long. It reads the next 10 bytes of
the record and then finds the third record is 12 bytes long and so on.
11 4iapps.com
Stream Record format
12 4iapps.com
Log File
The log file is a record of SQL*Loader’s activities during a load session. It contains information such as the following:
The names of the control file, log file, bad file, discard file, and data file
A detailed breakdown of the fields and datatypes in the data file that was loaded
A summary of the load that includes the number of logical records read from the data file, the number of rows rejected
because of errors, the number of rows discarded because of selection criteria, and the elapsed time of the load
Always review the log file after a load to be sure that no errors occurred, or at least that no unexpected errors
occurred. This type of information is written to the log file, but is not displayed on the terminal screen.
13 4iapps.com
Bad file
Whenever you insert data into a database, you run the risk of that insert failing because of some types of error.
Integrity constraint violations undoubtedly represent the most common type of error. However, other problems,
such as the lack of free space in a tablespace, can also cause insert operations to fail. Whenever SQL*Loader
encounters a database error while trying to load a record, it writes that record to a file known as the bad file.
If one or more records are rejected, the bad file is created and the rejected records are logged.
If no records are rejected, then the bad file is not created.
14 4iapps.com
Discard file
While SQL*Loader is being executed it creates a discard file for records that do not meet any of the loading
criteria. The records contained in this file are called discarded records. Discarded records do not satisfy any of the
WHEN clauses specified in the control file. These records differ from rejected records. Discarded records do not
necessarily have any bad data. A discarded record is never inserted into the Oracle table.
A discard file is created according to the following rules:
You have specified a discard filename and one or more records fail to satisfy all of the WHEN clauses specified in
the control file. (If the discard file is created, it overwrites any existing file with the same name, so be sure that you
do not overwrite any files that you want to retain.)
If no records are discarded, then a discard file is not created.
15 4iapps.com
Registering SQL*Loader as a Concurrent Program –
Step 1] Create Table, Control File and Data File
The following steps will describe the process to register a SQL*Loader program as a Concurrent Program in Oracle
Apps.
Step 1] Create Table, Control File and Data File
Create the SQL*Loader Control and Data file and place them in Server Custom Top/bin folder (eg: $XX4I_TOP/bin).
Create or check the interface table structures in the backend.
Create the table in which data are to be loaded. Sample script is given below.
16 4iapps.com
Control file
17 4iapps.com
Data file
18 4iapps.com
Step 2 - Register Executable
Go to Application Developer > Concurrent > Executables. Define a Concurrent Program Executable. Choose the Execution
Method as SQL*Loader and give the Execution File Name as the name of the SQL*Loader control file. Save your work.
19 4iapps.com
Step 3 - Register Concurrent Program
Go to Application Developer > Concurrent > Program. Define the Concurrent Program. Attach the executable defined above .
20 4iapps.com
Step 4 - Create Parameter
Go to parameters of the concurrent program. Create a parameter to take the server path of the data file. You can also place the
default value.
21 4iapps.com
Final steps..
22 4iapps.com
Case 1: Loading Variable-Length Data
A simple control file identifying one table and three columns to be loaded.
Control File
1) LOAD DATA
2) INFILE *
6) BEGINDATA
12,RESEARCH,"SARATOGA"
10,"ACCOUNTING",CLEVELAND
11,"ART",SALEM
13,FINANCE,"BOSTON"
21,"SALES",PHILA.
22,"SALES",ROCHESTER
42,"INT'L","SAN FRAN"
23 4iapps.com
Control file..
The LOAD DATA statement is required at the beginning of the control file.
INFILE * specifies that the data is found in the control file and not in an external file.
The INTO TABLE statement is required to identify the table to be loaded (DEPT) into. By default, SQL*Loader requires the
table to be empty before it inserts any records.
FIELDS TERMINATED BY specifies that the data is terminated by commas, but may also be enclosed by quotation marks.
Datatypes for all fields default to CHAR.
Specifies that the names of columns to load are enclosed in parentheses. Since no datatype is specified, the default is a
character of length 255.
BEGINDATA specifies the beginning of the data
24 4iapps.com
Invoking SQL*Loader
SQL*Loader loads the DEPT table and creates the log file.
Additional Information: The command "sqlldr" is a UNIX-specific invocation. To invoke SQL*Loader on your
operating system, refer to your Oracle operating system-specific documentation.
25 4iapps.com
Log file portion…
26 4iapps.com
Log file portion…
Log..
Table DEPT, loaded from every logical record.
Insert option in effect for this table: INSERT
Table DEPT:
7 Rows successfully loaded.
0 Rows not loaded due to data errors.
0 Rows not loaded because all WHEN clauses were failed.
0 Rows not loaded because all fields were null.
27 4iapps.com
Log file portion…
28 4iapps.com
Queries
29 4iapps.com
Thank you
[email protected]
1-1-1 Tecci Park PO Box No: 42693 Knowledge Oasis Muscat Al Sadd Area, Doha 18210 Contour Rd #202
285, Rajiv Gandhi Salai Hamriyah Free Zone – Sharjah Sultanate of Oman Qatar Montgomery Village MD 20877
Sholinganallur United Arab Emirates
Chennai - 600 119
Tel +91 44 6638 0000 Tel +971 6526 9057 Tel +968 9780 7119 Tel +974 4432 6634 Tel +1 240 252 5913
30 4iapps.com