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

SQL Loader

Uploaded by

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

SQL Loader

Uploaded by

naren312suren
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

4 i A p p s S o l u ti o n s

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

Log SQL*Loader Bad


Bad
file files
files

Bad
Bad
files
files

4 4iapps.com
Execution Steps

 Executes from the command prompt or parameter file.


 Reads the control file.
 Reads the data from the control file or from one or more data files.
 Loads the data in the database and logs the information in the log file.
 Places the rejected records in the bad file.
 Places the discarded records in the discard file.

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..

B] Table and Field_List Information:


 The INTO TABLE statement specifies the table into which data should be loaded. In this case it is
the Dept table. 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 can also be
enclosed by quotation marks. Data types for all fields default to CHAR.
 The names of columns to load are enclosed in parentheses. Because no data type or length is
specified, the default is type CHAR with a maximum length of 255.
C] Input Data:
 BEGINDATA specifies the beginning of the data. The data to be loaded is present below the
BEGINDATA command.

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

Stream Record Format:


 A file is in the stream record format when the records are not specified by size; instead SQL*Loader
forms records by scanning for the record terminator. Stream record format is the most flexible format, but
there can be a negative effect on performance. The syntax for specifying the stream record format is as
follows:

INFILE datafile_name ["str terminator_string"]


 The terminator_string can be a ‘char_string’ which is a string of characters enclosed in single or double
quotation marks or a ‘hex_string’ which is a byte string in hexadecimal 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

 The values of several command-line parameters

 A detailed breakdown of the fields and datatypes in the data file that was loaded

 Error messages for records that cause errors

 Messages indicating when records have been discarded

 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.

CREATE TABLE XX4I_DEPARTMENTS


(
DEPT_NO VARCHAR2(10 BYTE),
DEPT_NAME VARCHAR2(100 BYTE),
DEPT_EFF_START_DATE DATE,
DEPT_EFF_END_DATE DATE )

16 4iapps.com
Control file

Below is the content of the Control File: XX4IDEPT.ctl

17 4iapps.com
Data file

Below is the Data File XX4IDEPT.csv, which is to be


loaded.

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..

Step 5] Attach to Request Group


Step 6] Run Concurrent Program
 Go to that Responsibility and Run the Concurrent Program. If successful check the output file that have all data
uploading information.
Step 7] Check the data in the Table
 Check in the backend whether the tables got updated or not.
 The Bad and Discard files will be created in $COMMON_TOP/admin/out/<SID_server> folder.

22 4iapps.com
Case 1: Loading Variable-Length Data

A simple control file identifying one table and three columns to be loaded.

Control File

The control file is ULCASE1.CTL:

1) LOAD DATA

2) INFILE *

3) INTO TABLE dept

4) FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'

5) (deptno, dname, loc)

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

To run this example, invoke SQL*Loader with the command:


 sqlldr userid=scott/tiger control=ulcase1.ctl log=ulcase1.log

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…

 The following shows a portion of the log file:

Control File: ulcase1.ctl


Data File: ulcase1.ctl
Bad File: ulcase1.bad
Discard File: none specified
(Allow all discards)
Number to load: ALL
Number to skip: 0
Errors allowed: 50
Bind array: 64 rows, maximum of 65536 bytes
Continuation: none specified
Path used: Conventional

26 4iapps.com
Log file portion…

 Log..
Table DEPT, loaded from every logical record.
Insert option in effect for this table: INSERT

Column Name Position Len Term Encl Datatype


------------------------------ ---------- ----- ---- ---- ---------------------
1) DEPTNO FIRST * , O(") CHARACTER
DNAME NEXT * , O(") CHARACTER
2) LOC NEXT * , O(") CHARACTER

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.

Space allocated for bind array: 65016 bytes(84 rows)


Space allocated for memory besides bind array: 0 bytes

27 4iapps.com
Log file portion…

 Total logical records skipped: 0


 Total logical records read: 7
 Total logical records rejected: 0
 Total logical records discarded: 0

 Run began on Sun Nov 08 11:08:19 1998


 Run ended on Sun Nov 08 11:08:20 1998

 Elapsed time was: 00:00:01.16


 CPU time was: 00:00:00.10

28 4iapps.com
Queries

29 4iapps.com
Thank you
[email protected]

INDIA UAE OMAN QATAR USA

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

You might also like