SlideShare a Scribd company logo
SQL LOADER 
SQL LOADER utility is used to load data from other data source into Oracle. For 
example, if you have a table in FOXPRO, ACCESS or SYBASE or any other third 
party database, you can use SQL Loader to load the data into Oracle Tables. SQL 
Loader will only read the data from Flat files. So If you want to load the data 
from Foxpro or any other database, you have to first convert that data into 
Delimited Format flat file or Fixed length format flat file, and then use SQL loader 
to load the data into Oracle. 
Following is procedure to load the data from Third Party Database into Oracle 
using SQL Loader. 
1. Convert the Data into Flat file using third party database command. 
2. Create the Table Structure in Oracle Database using appropriate datatypes 
3. Write a Control File, describing how to interpret the flat file and options to 
load the data. 
4. Execute SQL Loader utility specifying the control file in the command line 
argument 
To understand it better let us see the following case study. 
CASE STUDY (Loading Data from MS-ACCESS to Oracle) 
Suppose you have a table in MS-ACCESS by name EMP, running under Windows 
O/S, with the following structure 
EMPNO INTEGER 
NAME TEXT(50) 
SAL CURRENCY 
JDATE DATE 
This table contains some 10,000 rows. Now you want to load the data from this 
table into an Oracle Table. Oracle Database is running in LINUX O/S. 
Solution 
Steps 
Start MS-Access and convert the table into comma delimited flat (popularly known 
as csv) , by clicking on File/Save Asmenu. Let the delimited file name 
be emp.csv
1. Now transfer this file to Linux Server using FTP command 
a. Go to Command Prompt in windows 
b. At the command prompt type FTP followed by IP address of the 
server running Oracle. 
FTP will then prompt you for username and password to connect to 
the Linux Server. Supply a valid username and password of Oracle 
User in Linux 
For example:- 
C:>ftp 200.200.100.111 
Name: oracle 
Password:oracle 
FTP> 
c. Now give PUT command to transfer file from current Windows 
machine to Linux machine. 
FTP>put 
Local file:C:>emp.csv 
remote-file:/u01/oracle/emp.csv 
File transferred in 0.29 Seconds 
FTP> 
d. Now after the file is transferred quit the FTP utility by 
typing bye command. 
FTP>bye 
Good-Bye 
2. Now come the Linux Machine and create a table in Oracle with the same 
structure as in MS-ACCESS by taking appropriate datatypes. For 
example, create a table like this 
$sqlplus scott/tiger 
SQL>CREATE TABLE emp (empno number(5), 
name varchar2(50),
sal number(10,2), 
jdate date); 
3. After creating the table, you have to write a control file describing the 
actions which SQL Loader should do. You can use any text editor to write 
the control file. Now let us write a controlfile for our case study 
$vi emp.ctl 
1 LOAD DATA 
2 INFILE ‘/u01/oracle/emp.csv’ 
3 BADFILE ‘/u01/oracle/emp.bad’ 
4 DISCARDFILE ‘/u01/oracle/emp.dsc’ 
5 INSERT INTO TABLE emp 
6 FIELDS TERMINATED BY “,” OPTIONALLY ENCLOSED BY 
‘”’ TRAILING NULLCOLS 
7 (empno,name,sal,jdate date ‘mm/dd/yyyy’) 
Notes: 
(Do not write the line numbers, they are meant for explanation purpose) 
1. The LOAD DATA statement is required at the beginning of the control file. 
2. The INFILE option specifies where the input file is located 
3. Specifying BADFILE is optional. If you specify, then bad records found during loading will 
be stored in this file. 
4. Specifying DISCARDFILE is optional. If you specify, then records which do not meet 
a WHEN condition will be written to this file. 
5. You can use any of the following loading option 
1. INSERT : Loads rows only if the target table is empty 
2. APPEND: Load rows if the target table is empty or not. 
3. REPLACE: First deletes all the rows in the existing table and then, load rows. 
4. TRUNCATE: First truncates the table and then load rows.
6. This line indicates how the fields are separated in input file. Since in our case the fields are 
separated by “,” so we have specified “,” as the terminating char for fields. You can replace this by 
any char which is used to terminate fields. Some of the popularly use terminating characters are 
semicolon “;”, colon “:”, pipe “|” etc. TRAILING NULLCOLS means if the last 
column is null then treat this as null value, otherwise, SQL LOADER will treat the record as 
bad if the last column is null. 
7. In this line specify the columns of the target table. Note how do you specify format for Date 
columns 
4. After you have wrote the control file save it and then, call SQL Loader utility by 
typing the following command 
$sqlldr userid=scott/tiger control=emp.ctl 
log=emp.log 
After you have executed the above command SQL Loader will shows you the 
output describing how many rows it has loaded. 
The LOG option of sqlldr specifies where the log file of this sql loader session 
should be created. The log file contains all actions which SQL loader has 
performed i.e. how many rows were loaded, how many were rejected and how much 
time is taken to load the rows and etc. You have to view this file for any errors 
encountered while runningSQL Loader. 
CASE STUDY (Loading Data from Fixed Length file into Oracle) 
Suppose we have a fixed length format file containing employees data, as shown 
below, and wants to load this data into an Oracle table. 
7782 CLARK MANAGER 7839 2572.50 10 
7839 KING PRESIDENT 5500.00 10 
7934 MILLER CLERK 7782 920.00 10 
7566 JONES MANAGER 7839 3123.75 20 
7499 ALLEN SALESMAN 7698 1600.00 300.00 30 
7654 MARTIN SALESMAN 7698 1312.50 1400.00 30 
7658 CHAN ANALYST 7566 3450.00 20 
7654 MARTIN SALESMAN 7698 1312.50 1400.00 30 
SOLUTION: 
Steps :-
1. First Open the file in a text editor and count the length of fields, for 
example in our fixed length file, employee number is from 1st position to 
4th position, employee name is from 6th position to 15th position, Job 
name is from 17th position to 25th position. Similarly other columns are 
also located. 
2. Create a table in Oracle, by any name, but should match columns 
specified in fixed length file. In our case give the following command to 
create the table. 
SQL> CREATE TABLE emp (empno NUMBER(5), 
name VARCHAR2(20), 
job VARCHAR2(10), 
mgr NUMBER(5), 
sal NUMBER(10,2), 
comm NUMBER(10,2), 
deptno NUMBER(3) ); 
3. After creating the table, now write a control file by using any text editor 
$vi empfix.ctl 
1) LOAD DATA 
2) INFILE '/u01/oracle/fix.dat' 
3) INTO TABLE emp 
4) (empno POSITION(01:04) INTEGER 
EXTERNAL, 
name POSITION(06: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, 
5) deptno POSITION(50:51) INTEGER 
EXTERNAL) 
Notes: 
(Do not write the line numbers, they are meant for explanation 
purpose) 
1. The LOAD DATA statement is required at the beginning of the control file. 
2. The name of the file containing data follows the INFILE parameter. 
3. The INTO TABLE statement is required to identify the table to be loaded into. 
4. Lines 4 and 5 identify a column name and the location of the data in the datafile to be loaded into 
that column. empno, name, job, and so on are names of columns in table emp. The datatypes 
(INTEGER EXTERNAL, CHAR, DECIMAL EXTERNAL) identify the datatype of data fields in 
the file, not of corresponding columns in the emp table. 
5. Note that the set of column specifications is enclosed in parentheses. 
4. After saving the control file now start SQL Loader utility by typing the 
following command. 
$sqlldr userid=scott/tiger control=empfix.ctl 
log=empfix.log direct=y 
After you have executed the above command SQL Loader will shows you the 
output describing how many rows it has loaded.
Loading Data into Multiple Tables using WHEN condition 
You can simultaneously load data into multiple tables in the same session. You can 
also use WHEN condition to load only specified rows which meets a particular 
condition (only equal to “=” and not equal to “<>” conditions are allowed). 
For example, suppose we have a fixed length file as shown below 
7782 CLARK MANAGER 7839 2572.50 10 
7839 KING PRESIDENT 5500.00 10 
7934 MILLER CLERK 7782 920.00 10 
7566 JONES MANAGER 7839 3123.75 20 
7499 ALLEN SALESMAN 7698 1600.00 300.00 30 
7654 MARTIN SALESMAN 7698 1312.50 1400.00 30 
7658 CHAN ANALYST 7566 3450.00 20 
7654 MARTIN SALESMAN 7698 1312.50 1400.00 30 
Now we want to load all the employees whose deptno is 10 into emp1 table and 
those employees whose deptno is not equal to 10 in emp2 table. To do this first 
create the tables emp1 and emp2 by taking appropriate columns and datatypes. 
Then, write a control file as shown below 
$vi emp_multi.ctl 
Load Data 
infile ‘/u01/oracle/empfix.dat’ 
append into table scott.emp1 
WHEN (deptno=’10 ‘) 
(empno POSITION(01:04) INTEGER 
EXTERNAL, 
name POSITION(06: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) 
INTO TABLE scott.emp2 
WHEN (deptno<>’10 ‘) 
(empno POSITION(01:04) INTEGER 
EXTERNAL, 
name POSITION(06: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) 
After saving the file emp_multi.ctl run sqlldr 
$sqlldr userid=scott/tiger control=emp_multi.ctl 
Conventional Path Load and Direct Path Load. 
SQL Loader can load the data into Oracle database using Conventional Path 
method or Direct Path method. You can specify the method by 
using DIRECT command line option. If you give DIRECT=TRUE then SQL loader 
will use Direct Path Loading otherwise, if omit this option or 
specify DIRECT=false, then SQL Loader will use Conventional Path loading 
method.
Conventional Path 
Conventional path load (the default) uses the SQL INSERT statement and a bind 
array buffer to load data into database tables. 
When SQL*Loader performs a conventional path load, it competes equally with all 
other processes for buffer resources. This can slow the load significantly. Extra 
overhead is added as SQL statements are generated, passed to Oracle, and 
executed. 
The Oracle database looks for partially filled blocks and attempts to fill them on 
each insert. Although appropriate during normal use, this can slow bulk loads 
dramatically. 
Direct Path 
In Direct Path Loading, Oracle will not use SQL INSERT statement for loading 
rows. Instead it directly writes the rows, into fresh blocks beyond High 
Water Mark, in datafiles i.e. it does not scan for free blocks before high water 
mark. Direct Path load is very fast because 
 Partial blocks are not used, so no reads are needed to find them, and fewer writes are 
performed. 
 SQL*Loader need not execute any SQL INSERT statements; therefore, the processing load 
on the Oracle database is reduced. 
 A direct path load calls on Oracle to lock tables and indexes at the start of the load and 
releases them when the load is finished. A conventional path load calls Oracle once for each 
array of rows to process a SQL INSERT statement. 
 A direct path load uses multiblock asynchronous I/O for writes to the database files. 
 During a direct path load, processes perform their own write I/O, instead of using Oracle's 
buffer cache. This minimizes contention with other Oracle users. 
Restrictions on Using Direct Path Loads 
The following conditions must be satisfied for you to use the direct path load 
method: 
 Tables are not clustered. 
 Tables to be loaded do not have any active transactions pending. 
 Loading a parent table together with a child Table 
 Loading BFILE columns
Sql loader good example

More Related Content

What's hot

12 multi-threading
12 multi-threading12 multi-threading
12 multi-threading
APU
 
PL/SQL Code for Sample Projects
PL/SQL Code for Sample ProjectsPL/SQL Code for Sample Projects
PL/SQL Code for Sample Projects
jwjablonski
 

What's hot (20)

Database Objects
Database ObjectsDatabase Objects
Database Objects
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQL
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
Oracle Form material
Oracle Form materialOracle Form material
Oracle Form material
 
10 Creating Triggers
10 Creating Triggers10 Creating Triggers
10 Creating Triggers
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
PL/SQL
PL/SQLPL/SQL
PL/SQL
 
Basic SQL and History
 Basic SQL and History Basic SQL and History
Basic SQL and History
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
Sql views
Sql viewsSql views
Sql views
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 
Sql
SqlSql
Sql
 
4. plsql
4. plsql4. plsql
4. plsql
 
07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 
Oracle pl/sql control statments
Oracle pl/sql control statmentsOracle pl/sql control statments
Oracle pl/sql control statments
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threading
 
PL/SQL Code for Sample Projects
PL/SQL Code for Sample ProjectsPL/SQL Code for Sample Projects
PL/SQL Code for Sample Projects
 
Extensions in OAF
Extensions in OAF Extensions in OAF
Extensions in OAF
 
Chapter 4 Structured Query Language
Chapter 4 Structured Query LanguageChapter 4 Structured Query Language
Chapter 4 Structured Query Language
 

Similar to Sql loader good example

ECET 450 Laboratory 2Lab 2 involves two parts.Part A involv.docx
ECET 450 Laboratory 2Lab 2 involves two parts.Part A involv.docxECET 450 Laboratory 2Lab 2 involves two parts.Part A involv.docx
ECET 450 Laboratory 2Lab 2 involves two parts.Part A involv.docx
jenkinsmandie
 
My sql with querys
My sql with querysMy sql with querys
My sql with querys
NIRMAL FELIX
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
Alessandro Baratella
 
DataPump ile Single Parititon Export
DataPump ile Single Parititon ExportDataPump ile Single Parititon Export
DataPump ile Single Parititon Export
Anar Godjaev
 

Similar to Sql loader good example (20)

Oracle sql loader utility
Oracle sql loader utilityOracle sql loader utility
Oracle sql loader utility
 
Sqlldr examples
Sqlldr examplesSqlldr examples
Sqlldr examples
 
Editdmpfile
EditdmpfileEditdmpfile
Editdmpfile
 
Dynamic websites lec3
Dynamic websites lec3Dynamic websites lec3
Dynamic websites lec3
 
Oracle notes
Oracle notesOracle notes
Oracle notes
 
My sql.ppt
My sql.pptMy sql.ppt
My sql.ppt
 
Data Definition Language (DDL)
Data Definition Language (DDL) Data Definition Language (DDL)
Data Definition Language (DDL)
 
ECET 450 Laboratory 2Lab 2 involves two parts.Part A involv.docx
ECET 450 Laboratory 2Lab 2 involves two parts.Part A involv.docxECET 450 Laboratory 2Lab 2 involves two parts.Part A involv.docx
ECET 450 Laboratory 2Lab 2 involves two parts.Part A involv.docx
 
WEB TECHNOLOGIES Unit 2
WEB TECHNOLOGIES Unit 2WEB TECHNOLOGIES Unit 2
WEB TECHNOLOGIES Unit 2
 
My sql with querys
My sql with querysMy sql with querys
My sql with querys
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
Etl2
Etl2Etl2
Etl2
 
NOTES ON "FOXPRO"
NOTES ON "FOXPRO" NOTES ON "FOXPRO"
NOTES ON "FOXPRO"
 
Createclone
CreatecloneCreateclone
Createclone
 
Peoplesoft installation steps
Peoplesoft installation stepsPeoplesoft installation steps
Peoplesoft installation steps
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
Rman cloning guide
Rman cloning guideRman cloning guide
Rman cloning guide
 
DataPump ile Single Parititon Export
DataPump ile Single Parititon ExportDataPump ile Single Parititon Export
DataPump ile Single Parititon Export
 
An overview of snowflake
An overview of snowflakeAn overview of snowflake
An overview of snowflake
 

Recently uploaded

AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
Alluxio, Inc.
 

Recently uploaded (20)

TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 

Sql loader good example

  • 1. SQL LOADER SQL LOADER utility is used to load data from other data source into Oracle. For example, if you have a table in FOXPRO, ACCESS or SYBASE or any other third party database, you can use SQL Loader to load the data into Oracle Tables. SQL Loader will only read the data from Flat files. So If you want to load the data from Foxpro or any other database, you have to first convert that data into Delimited Format flat file or Fixed length format flat file, and then use SQL loader to load the data into Oracle. Following is procedure to load the data from Third Party Database into Oracle using SQL Loader. 1. Convert the Data into Flat file using third party database command. 2. Create the Table Structure in Oracle Database using appropriate datatypes 3. Write a Control File, describing how to interpret the flat file and options to load the data. 4. Execute SQL Loader utility specifying the control file in the command line argument To understand it better let us see the following case study. CASE STUDY (Loading Data from MS-ACCESS to Oracle) Suppose you have a table in MS-ACCESS by name EMP, running under Windows O/S, with the following structure EMPNO INTEGER NAME TEXT(50) SAL CURRENCY JDATE DATE This table contains some 10,000 rows. Now you want to load the data from this table into an Oracle Table. Oracle Database is running in LINUX O/S. Solution Steps Start MS-Access and convert the table into comma delimited flat (popularly known as csv) , by clicking on File/Save Asmenu. Let the delimited file name be emp.csv
  • 2. 1. Now transfer this file to Linux Server using FTP command a. Go to Command Prompt in windows b. At the command prompt type FTP followed by IP address of the server running Oracle. FTP will then prompt you for username and password to connect to the Linux Server. Supply a valid username and password of Oracle User in Linux For example:- C:>ftp 200.200.100.111 Name: oracle Password:oracle FTP> c. Now give PUT command to transfer file from current Windows machine to Linux machine. FTP>put Local file:C:>emp.csv remote-file:/u01/oracle/emp.csv File transferred in 0.29 Seconds FTP> d. Now after the file is transferred quit the FTP utility by typing bye command. FTP>bye Good-Bye 2. Now come the Linux Machine and create a table in Oracle with the same structure as in MS-ACCESS by taking appropriate datatypes. For example, create a table like this $sqlplus scott/tiger SQL>CREATE TABLE emp (empno number(5), name varchar2(50),
  • 3. sal number(10,2), jdate date); 3. After creating the table, you have to write a control file describing the actions which SQL Loader should do. You can use any text editor to write the control file. Now let us write a controlfile for our case study $vi emp.ctl 1 LOAD DATA 2 INFILE ‘/u01/oracle/emp.csv’ 3 BADFILE ‘/u01/oracle/emp.bad’ 4 DISCARDFILE ‘/u01/oracle/emp.dsc’ 5 INSERT INTO TABLE emp 6 FIELDS TERMINATED BY “,” OPTIONALLY ENCLOSED BY ‘”’ TRAILING NULLCOLS 7 (empno,name,sal,jdate date ‘mm/dd/yyyy’) Notes: (Do not write the line numbers, they are meant for explanation purpose) 1. The LOAD DATA statement is required at the beginning of the control file. 2. The INFILE option specifies where the input file is located 3. Specifying BADFILE is optional. If you specify, then bad records found during loading will be stored in this file. 4. Specifying DISCARDFILE is optional. If you specify, then records which do not meet a WHEN condition will be written to this file. 5. You can use any of the following loading option 1. INSERT : Loads rows only if the target table is empty 2. APPEND: Load rows if the target table is empty or not. 3. REPLACE: First deletes all the rows in the existing table and then, load rows. 4. TRUNCATE: First truncates the table and then load rows.
  • 4. 6. This line indicates how the fields are separated in input file. Since in our case the fields are separated by “,” so we have specified “,” as the terminating char for fields. You can replace this by any char which is used to terminate fields. Some of the popularly use terminating characters are semicolon “;”, colon “:”, pipe “|” etc. TRAILING NULLCOLS means if the last column is null then treat this as null value, otherwise, SQL LOADER will treat the record as bad if the last column is null. 7. In this line specify the columns of the target table. Note how do you specify format for Date columns 4. After you have wrote the control file save it and then, call SQL Loader utility by typing the following command $sqlldr userid=scott/tiger control=emp.ctl log=emp.log After you have executed the above command SQL Loader will shows you the output describing how many rows it has loaded. The LOG option of sqlldr specifies where the log file of this sql loader session should be created. The log file contains all actions which SQL loader has performed i.e. how many rows were loaded, how many were rejected and how much time is taken to load the rows and etc. You have to view this file for any errors encountered while runningSQL Loader. CASE STUDY (Loading Data from Fixed Length file into Oracle) Suppose we have a fixed length format file containing employees data, as shown below, and wants to load this data into an Oracle table. 7782 CLARK MANAGER 7839 2572.50 10 7839 KING PRESIDENT 5500.00 10 7934 MILLER CLERK 7782 920.00 10 7566 JONES MANAGER 7839 3123.75 20 7499 ALLEN SALESMAN 7698 1600.00 300.00 30 7654 MARTIN SALESMAN 7698 1312.50 1400.00 30 7658 CHAN ANALYST 7566 3450.00 20 7654 MARTIN SALESMAN 7698 1312.50 1400.00 30 SOLUTION: Steps :-
  • 5. 1. First Open the file in a text editor and count the length of fields, for example in our fixed length file, employee number is from 1st position to 4th position, employee name is from 6th position to 15th position, Job name is from 17th position to 25th position. Similarly other columns are also located. 2. Create a table in Oracle, by any name, but should match columns specified in fixed length file. In our case give the following command to create the table. SQL> CREATE TABLE emp (empno NUMBER(5), name VARCHAR2(20), job VARCHAR2(10), mgr NUMBER(5), sal NUMBER(10,2), comm NUMBER(10,2), deptno NUMBER(3) ); 3. After creating the table, now write a control file by using any text editor $vi empfix.ctl 1) LOAD DATA 2) INFILE '/u01/oracle/fix.dat' 3) INTO TABLE emp 4) (empno POSITION(01:04) INTEGER EXTERNAL, name POSITION(06:15) CHAR, job POSITION(17:25) CHAR,
  • 6. mgr POSITION(27:30) INTEGER EXTERNAL, sal POSITION(32:39) DECIMAL EXTERNAL, comm POSITION(41:48) DECIMAL EXTERNAL, 5) deptno POSITION(50:51) INTEGER EXTERNAL) Notes: (Do not write the line numbers, they are meant for explanation purpose) 1. The LOAD DATA statement is required at the beginning of the control file. 2. The name of the file containing data follows the INFILE parameter. 3. The INTO TABLE statement is required to identify the table to be loaded into. 4. Lines 4 and 5 identify a column name and the location of the data in the datafile to be loaded into that column. empno, name, job, and so on are names of columns in table emp. The datatypes (INTEGER EXTERNAL, CHAR, DECIMAL EXTERNAL) identify the datatype of data fields in the file, not of corresponding columns in the emp table. 5. Note that the set of column specifications is enclosed in parentheses. 4. After saving the control file now start SQL Loader utility by typing the following command. $sqlldr userid=scott/tiger control=empfix.ctl log=empfix.log direct=y After you have executed the above command SQL Loader will shows you the output describing how many rows it has loaded.
  • 7. Loading Data into Multiple Tables using WHEN condition You can simultaneously load data into multiple tables in the same session. You can also use WHEN condition to load only specified rows which meets a particular condition (only equal to “=” and not equal to “<>” conditions are allowed). For example, suppose we have a fixed length file as shown below 7782 CLARK MANAGER 7839 2572.50 10 7839 KING PRESIDENT 5500.00 10 7934 MILLER CLERK 7782 920.00 10 7566 JONES MANAGER 7839 3123.75 20 7499 ALLEN SALESMAN 7698 1600.00 300.00 30 7654 MARTIN SALESMAN 7698 1312.50 1400.00 30 7658 CHAN ANALYST 7566 3450.00 20 7654 MARTIN SALESMAN 7698 1312.50 1400.00 30 Now we want to load all the employees whose deptno is 10 into emp1 table and those employees whose deptno is not equal to 10 in emp2 table. To do this first create the tables emp1 and emp2 by taking appropriate columns and datatypes. Then, write a control file as shown below $vi emp_multi.ctl Load Data infile ‘/u01/oracle/empfix.dat’ append into table scott.emp1 WHEN (deptno=’10 ‘) (empno POSITION(01:04) INTEGER EXTERNAL, name POSITION(06:15) CHAR, job POSITION(17:25) CHAR, mgr POSITION(27:30) INTEGER EXTERNAL,
  • 8. sal POSITION(32:39) DECIMAL EXTERNAL, comm POSITION(41:48) DECIMAL EXTERNAL, deptno POSITION(50:51) INTEGER EXTERNAL) INTO TABLE scott.emp2 WHEN (deptno<>’10 ‘) (empno POSITION(01:04) INTEGER EXTERNAL, name POSITION(06: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) After saving the file emp_multi.ctl run sqlldr $sqlldr userid=scott/tiger control=emp_multi.ctl Conventional Path Load and Direct Path Load. SQL Loader can load the data into Oracle database using Conventional Path method or Direct Path method. You can specify the method by using DIRECT command line option. If you give DIRECT=TRUE then SQL loader will use Direct Path Loading otherwise, if omit this option or specify DIRECT=false, then SQL Loader will use Conventional Path loading method.
  • 9. Conventional Path Conventional path load (the default) uses the SQL INSERT statement and a bind array buffer to load data into database tables. When SQL*Loader performs a conventional path load, it competes equally with all other processes for buffer resources. This can slow the load significantly. Extra overhead is added as SQL statements are generated, passed to Oracle, and executed. The Oracle database looks for partially filled blocks and attempts to fill them on each insert. Although appropriate during normal use, this can slow bulk loads dramatically. Direct Path In Direct Path Loading, Oracle will not use SQL INSERT statement for loading rows. Instead it directly writes the rows, into fresh blocks beyond High Water Mark, in datafiles i.e. it does not scan for free blocks before high water mark. Direct Path load is very fast because  Partial blocks are not used, so no reads are needed to find them, and fewer writes are performed.  SQL*Loader need not execute any SQL INSERT statements; therefore, the processing load on the Oracle database is reduced.  A direct path load calls on Oracle to lock tables and indexes at the start of the load and releases them when the load is finished. A conventional path load calls Oracle once for each array of rows to process a SQL INSERT statement.  A direct path load uses multiblock asynchronous I/O for writes to the database files.  During a direct path load, processes perform their own write I/O, instead of using Oracle's buffer cache. This minimizes contention with other Oracle users. Restrictions on Using Direct Path Loads The following conditions must be satisfied for you to use the direct path load method:  Tables are not clustered.  Tables to be loaded do not have any active transactions pending.  Loading a parent table together with a child Table  Loading BFILE columns