SQL*Loader in Oracle 12c
SQL*Loader is a utility provided by the Oracle to load the data into Oracle databases from various files like ascii files,textfiles,external files and csv files etc..,
SQL*Loader provides two methods for loading data:
- conventional path load
- direct path load
CONVENTIONAL PATH LOAD
Conventional path loads (the default) use the SQL command INSERT and a bind array buffer to load data into database tables. This method is used by all Oracle tools and applications.
Oracle looks for partially filled blocks and attempts to fill them on each insert. Although appropriate during normal use, this can slow bulk loads dramatically.
In conventional path extra overhead is added as SQL commands are generated, passed to Oracle, and processed.
DIRECT PATH LOAD
Instead of filling a bind array buffer and passing it to Oracle with a SQL INSERT command, the direct path option creates data blocks that are already in Oracle database block format. These database blocks are then written directly to the database.
The direct path does not contend for free buffers in the buffer cache.
Partial blocks are not used, so no reads are needed to find them and fewer writes are performed.
SQL calls are not performed anytime during the load.
Files related to the sql*loader :
- Input file (or) Infile
- Controlfile
- Bad file
- Discard file
- Log file
Input File (or) Infile :
Infile is the name of data source file like .csv or .txt which will be given input to a control file to look for the data.
Control file :
Control file describes the action to be done by the sql loader and we can use any text editor to writing a controlfile.
Usually this will be followed by .ctl file extension.
Bad File :
Bad file stores the records which got failed during the loading operation.
Simply it stores the records which are rejected by the Oracle.
Example : Table has primary key, datatype mismatch etc…,
Bad file is a optional file.
Discard file :
Discard file stores the records which are rejected by user during loading operation.
when user specified when or where condition as a part of control file, As per the condition few records are rejected and stored in discard file.
Logfile :
Logfile has summary of loading operation like
How many lines are successfully loaded.
How many lines are written to discard file.
How lines are written to bad file.
Data Loading Options :
INSERT :
Specifies that your loading into an empty table. Sql Loader will abort the load if the table contains data to start with
Insert is the default option.
APPEND :
Append is used to load the data if table is already have the records.
REPLACE :
Replace specifies that, we want to override the data in the table before loading
Simple replace will delete all the rows from a table before loading.
TRUNCATE :
Truncate is same like as Replace, But SQL*Loader uses TRUNCATE instead of DELETE command.
Valid Keywords in SQL*Loader
userid — ORACLE username/password
control — 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 (Default all)
skip — number of logical records to skip (Default 0)
load — number of logical records to load (Default all)
errors — number of errors to allow (Default 50)
rows — number of rows in conventional path bind array or between direct path data saves(Default: Conventional path 64, Direct path all)
bindsize — size of conventional path bind array in bytes (Default 256000)
silent — suppress messages during run (header,feedback,errors,discards,partitions)
direct — use direct path (Default FALSE)
parfile — parameter file: name of file that contains parameter specifications
parallel — do parallel load (Default FALSE)
file — file to allocate extents from
skip_unusable_indexes — disallow/allow unusable indexes or index partitions(Default FALSE)
skip_index_maintenance — do not maintain indexes, mark affected indexes as unusable (Default FALSE)
commit_discontinued — commit loaded rows when load is discontinued (Default FALSE)
readsize — size of read buffer (Default 1048576)
external_table — use external table for load; NOT_USED, GENERATE_ONLY, EXECUTE (Default NOT_USED)
streamsize — size of direct path stream buffer in bytes (Default 256000)
multithreading — use multithreading in direct path
resumable — enable or disable resumable for current session (Default FALSE)
resumable_name — text string to help identify resumable statement
resumable_timeout — wait time (in seconds) for RESUMABLE (Default 7200)
date_cache — size (in entries) of date conversion cache (Default 1000)
Some examples on SQL*Loader :
Example 1 :
In this example,we are loading the data into dept table(empty table) using insert command.
Step 1: Create a table with required columns and datatypes.
1 2 3 4 |
AJAY>>select * from tab; no rows selected AJAY>>create table dept(deptno number,dname varchar2(10),loc varchar2(10)); Table created. |
Step 2 : Check input datafile and control file( Verified file format .csv or .txt)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[oracle@dba12 sqlldr]$ cat case.dat 21,SCIENCE,HOSTON 22,MATHEMATICS,TEXAS 23,ZOOLOGY,VEGAS 24,BOTONY,NEWYORK [oracle@dba12 sqlldr]$ cat case.ctl load data infile 'case1.dat' badfile 'case1.bad' discardfile 'case1.disc' insert into table dept fields terminated by ',' (deptno,dname,loc) |
Step 3 : Load the data using SQLLDR utility by specifying userid and control values.
1 2 3 4 5 6 7 8 9 10 |
[oracle@dba12 sqlldr]$ sqlldr userid=ajay/ajay control=case.ctl SQL*Loader: Release 12.1.0.2.0 - Production on Wed Jun 20 21:40:51 2018 Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved. Path used: Conventional Commit point reached - logical record count 4 Table DEPT: 4 Rows successfully loaded. Check the log file: case.log for more information about the load. |
Step 4 : Check the dept table
1 2 3 4 5 6 7 8 |
AJAY>>select * from dept; DEPTNO DNAME LOC ------- ---------- ----------- 21 SCIENCE HOUSTON 22 MATHEMATICS TEXAS 23 ZOOLOGY VEGAS 24 BOTONY NEW YORK |
Step 5 : Check the logfile information.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
[oracle@dba12 sqlldr]$ cat case.log SQL*Loader: Release 12.1.0.2.0 - Production on Wed Jun 20 21:40:51 2018 Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved. Control File: case.ctl Data File: case1.dat Bad File: case1.bad Discard File: case1.disc (Allow all discards) Number to load: ALL Number to skip: 0 Errors allowed: 50 Bind array: 64 rows, maximum of 256000 bytes Continuation: none specified Path used: Conventional Table DEPT, loaded from every logical record. Insert option in effect for this table: INSERT Column Name Position Len Term Encl Datatype ------------------------------ ---------- ----- ---- ---- --------------------- DEPTNO FIRST * , CHARACTER DNAME NEXT * , CHARACTER LOC NEXT * , CHARACTER Table DEPT: 4 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: 49536 bytes(64 rows) Read buffer bytes: 1048576 Total logical records skipped: 0 Total logical records read: 4 Total logical records rejected: 0 Total logical records discarded: 0 Run began on Wed Jun 20 21:40:51 2018 Run ended on Wed Jun 20 21:40:51 2018 Elapsed time was: 00:00:00.50 CPU time was: 00:00:00.08 |
In log file ,we have following information
Files used during loading operation.
No of lines skipped,loaded and errors.
which type of path we used for loading operation.
How many records loaded into table.
How many records are rejected by Oracle.
How many records are rejected by user using when condition.
In case its conventional path it shows about bind array occupied size.
At what time loading started ,stopped,Elapsed time and CPU time.
Example 2 :
In this example,we are loading the data into dept table (which have existing records) using append command.
Check the existing table.
1 2 3 4 5 6 7 |
AJAY>>select * from dept; DEPTNO DNAME LOC ------- --------------- ---------- 21 SCIENCE HOSTON 22 MATHEMATICS TEXAS 23 ZOOLOGY VEGAS 24 BOTONY NEWYORK |
Check the infile or input file.
1 2 3 4 5 6 7 |
[oracle@dba12 sqlldr]$ vi case1.dat 21,SCIENCE,HOSTON 22,MATHEMATICS,TEXAS 23,ZOOLOGY,VEGAS 24,BOTONY,NEWYORK 25,PHYSICS,CA 26,GEOGRAPHY,CA |
Check the control file with specific when clause.
1 2 3 4 5 6 7 8 9 10 |
[oracle@dba12 sqlldr]$ vi case1.ctl load data infile 'case1.dat' badfile 'case1.bad' discardfile 'case1.disc' append into table dept when deptno ='25' fields terminated by ',' (deptno,dname,loc) |
Use Sqlldr utility to load the data into existing table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
[oracle@dba12 sqlldr]$ sqlldr userid=ajay/ajay control=case1.ctl SQL*Loader: Release 12.1.0.2.0 - Production on Mon Jun 25 18:56:29 2018 Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved. Path used: Conventional Commit point reached - logical record count 6 Table DEPT: 1 Row successfully loaded. Check the log file: case1.log for more information about the load. [oracle@dba12 sqlldr]$ sqlplus ajay/ajay SQL*Plus: Release 12.1.0.2.0 Production on Mon Jun 25 18:58:39 2018 Copyright (c) 1982, 2014, Oracle. All rights reserved. Last Successful login time: Mon Jun 25 2018 18:56:29 +05:30 Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options AJAY>>select * from dept; DEPTNO DNAME LOC ------- --------------- ---------- 21 SCIENCE HOSTON 22 MATHEMATICS TEXAS 23 ZOOLOGY VEGAS 24 BOTONY NEWYORK 25 PHYSICS CA |
Here only one row successfully added into dept table because of when clause.
Example 3 :
In this example,we are loading the data into dept table (which have existing records) using truncate command.
Truncate command internally performs truncate operation and then it will perform insert command to load the data.
1 2 3 4 5 6 7 |
[oracle@dba12 sqlldr]$ vi case2.dat 21,SCIENCE,HOSTON 22,MATHEMATICS,TEXAS 23,ZOOLOGY,VEGAS 24,BOTONY,NEWYORK 25,PHYSICS,CA 26,GEOGRAPHY,CA |
Check the control file with specific when clause.
1 2 3 4 5 6 7 8 9 |
[oracle@dba12 sqlldr]$ vi case2.ctl load data infile 'case1.dat' badfile 'case1.bad' discardfile 'case1.disc' truncate into table dept fields terminated by ',' (deptno,dname,loc) |
Use Sqlldr utility to load the data into existing table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[oracle@dba12 sqlldr]$ sqlldr userid=ajay/ajay control=case2.ctl SQL*Loader: Release 12.1.0.2.0 - Production on Mon Jun 25 18:56:29 2018 Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved. Path used: Conventional Commit point reached - logical record count 6 Table DEPT: 6 Row successfully loaded. Check the log file: case1.log for more information about the load. [oracle@dba12 sqlldr]$ sqlplus ajay/ajay SQL*Plus: Release 12.1.0.2.0 Production on Mon Jun 25 18:58:39 2018 Copyright (c) 1982, 2014, Oracle. All rights reserved. Last Successful login time: Mon Jun 25 2018 18:56:29 +05:30 Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options AJAY>>select * from dept; DEPTNO DNAME LOC ------- --------------- ---------- 21 SCIENCE HOSTON 22 MATHEMATICS TEXAS 23 ZOOLOGY VEGAS 24 BOTONY NEWYORK 25 PHYSICS CA 26 COMMERCE TEXAS |
Example 4 :
In this example we loading data into dept table using insert command but instead of specifying infile,the data has been stored in control file and we used positions instead of delimiters.
Create on empty emp table with required columns and datatypes.
1 2 3 4 5 6 7 8 9 10 |
AJAY>>create table emp (empno number,ename varchar2(10),sal number(7,2),comm number(7,2),ajay_col number(10)); Table created. AJAY>>desc emp Name Null? Type ----------------------------------------- -------- ---------------------------- EMPNO NUMBER ENAME VARCHAR2(10) SAL NUMBER(7,2) COMM NUMBER(7,2) AJAY_COL NUMBER(10) |
Check the control file which have data in that.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[oracle@dba12 sqlldr]$ cat case5.ctl load data infile * insert into table emp (empno position(1:4), ename position(5:10), sal position(11:14), comm position(15:17)) begindata 7767king 0083102 7765miller3439104 7765martin3145 |
Use Sqlldr utility to load the data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[oracle@dba12 sqlldr]$ sqlldr userid=ajay/ajay control=case5.ctl SQL*Loader: Release 12.1.0.2.0 - Production on Mon Jun 25 19:21:08 2018 Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved. Path used: Conventional Commit point reached - logical record count 3 Table EMP: 3 Rows successfully loaded. Check the log file: case5.log for more information about the load. AJAY>>select * from emp; EMPNO ENAME SAL COMM AJAY_COL ------ -------- ----- ----- ---------- 7767 king 83 102 7765 miller 3439 104 7765 martin 3145 |
Totally table have 4 columns , but here we loaded totally 3 records into four columns
Example 4 :
In this example we are loading the data into multiple tables using insert command.
Create two tables with the names emp2 & emp3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
AJAY>>CREATE TABLE EMP2(EMPNO NUMBER(4) NOT NULL,ENAME VARCHAR2(10),JOB VARCHAR2(9),MGR NUMBER(4),HIREDATE DATE, SAL NUMBER(7, 2),COMM NUMBER(7, 2),DEPTNO NUMBER(2)); Table created. AJAY>>CREATE TABLE EMP3 (EMPNO NUMBER(4) NOT NULL,ENAME VARCHAR2(10),JOB VARCHAR2(9),MGR NUMBER(4),HIREDATE DATE, SAL NUMBER(7, 2),COMM NUMBER(7, 2),DEPTNO NUMBER(2)); Table created. AJAY>>desc emp2 Name Null? Type ----------------------------------------- -------- ---------------------------- EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) MGR NUMBER(4) HIREDATE DATE SAL NUMBER(7,2) COMM NUMBER(7,2) DEPTNO NUMBER(2) AJAY>>desc emp3 Name Null? Type ----------------------------------------- -------- ---------------------------- EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) MGR NUMBER(4) HIREDATE DATE SAL NUMBER(7,2) COMM NUMBER(7,2) DEPTNO NUMBER(2) |
check the control file which have the commands.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[oracle@dba12 sqlldr]$ vi case6.ctl load data infile 'case6.dat' badfile 'case6.bad' discardfile 'case6.disc' truncate into table emp2 (empno position(1:4) integer external, ename position(6:15) char, deptno position(17:18) char, mgr position (20:23) integer external) into table emp3 (empno position(1:4), ename position(6:15), deptno position(17:18) char, mgr position (20:23) integer external) |
Check the infile which have the records.
1 2 3 4 5 |
[oracle@dba12 sqlldr]$ cat case6.dat 7777 jay 10 8888 8888 vinod 20 9999 9999 seetha 30 7777 |
Use sqlldr utility to load the tables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
[oracle@dba12 sqlldr]$ sqlldr userid=ajay/ajay control=case6.ctl SQL*Loader: Release 12.1.0.2.0 - Production on Mon Jun 25 19:58:08 2018 Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved. Path used: Conventional Commit point reached - logical record count 3 Table EMP2: 3 Rows successfully loaded. Table EMP3: 3 Rows successfully loaded. Check the log file: case6.log for more information about the load. AJAY>>select * from emp2; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ------ -------- --------- ----- --------- ----- ----- ------- 7777 jay 8888 10 8888 vinod 9999 20 9999 seetha 7777 30 AJAY>>select * from emp3; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ------ -------- --------- ----- --------- ----- ----- ------- 7777 jay 8888 10 8888 vinod 9999 20 9999 seetha 7777 30 |
Here three records loaded into emp2 and emp3 tables that to into specific columns.
Thanks for referring this article.
Ajay Kumar
If you are interested to know more details about future session please join below telegram group :
https://t.me/joinchat/JFVAtAv1TE9DGHLbJ6rZbw