21 Like 50 Tweet
If you are using Oracle database, at some point you might have to deal
with uploading data to the tables from a text file.
$ cat employee.txt
100,Thomas,Sales,5000
200,Jason,Technology,5500
300,Mayla,Technology,7000
400,Nisha,Marketing,9500
500,Randy,Technology,6000
501,Ritu,Accounting,5400
SQL*Loader Control File
This contains the instructions to the sqlldr utility. This tells sqlldr the
location of the input file, the format of the input file, and other optional
meta data information required by the sqlldr to upload the data into oracle
tables.
$ cat example1.ctl
load data
infile '/home/ramesh/employee.txt'
id integer,
name varchar2(10),
dept varchar2(15),
salary integer,
hiredon date
Next create the control file that explains what needs to be upload and
where.
$ cat sqlldr-add-new.ctl
load data
infile '/home/ramesh/employee.txt'
Note: If you have the values inside the data file enclosed with double quote,
use this in your control file: fields terminated by , optionally enclosed by
Note: If you dont have the table created, youll get the following error
message:
You can pass the userid and password to the sqlldr command using any
one of the following format. As you see below, both of these will prompt
you for control file location, as it was not given in the command line.
$ sqlldr scott/tiger
(or)
$ sqlldr userid=scott/tiger
control =
Execute the sqlldr command to upload these new record to the empty
table by specifying both uid/pwd and the control file location as shown
below.
This will create the output log file in the same name as the data file, but
with the .log extension (instead of .ctl). Partial output shown below.
$ cat sqlldr-add-new.log
Table EMPLOYEE:
5 Rows successfully loaded.
If you are new to Oracle database, and like to install it, follow this Oracle
11g installation guide.
$ vi newemployee.txt
600,Ritu,Accounting,5400
700,Jessica,Marketing,7800
If you create a similar control file like the previous example, you might get
the following error message.
SQL*Loader-601: For INSERT option, table must be empty. Error on table EMPLOYEE
The above indicates that the table should be empty before you can upload
data using sql*loader.
If you like to insert more data to the tables without having to delete the
existing rows, use the append command as shown in the following control
file.
$ vi sqlldr-append-more.ctl
load data
infile '/home/ramesh/newemployee.txt'
append
into table employee
$ cat sqlldr-add-new-with-data.ctl
load data
infile *
into table employee
begindata
100,Thomas,Sales,5000
200,Jason,Technology,5500
300,Mayla,Technology,7000
400,Nisha,Marketing,9500
500,Randy,Technology,6000
Note: The infile will say * in this case, as there is no input data file name
for this example.
$ cat employee-date.txt
100,Thomas$Sales^5000,31-JAN-2008
200,Jason$Technology^5500,01-Feb-2005
300,Mayla$Technology^7000,10-Aug-2000
400,Nisha$Marketing^9500,12-Dec-2011
500,Randy$Technology^6000,01-JAN-2007
Create the following control file and indicate the field delimiters for each
and every field using terminated by as shown below.
$ cat sqlldr-date.ctl
load data
infile '/home/ramesh/employee-date.txt'
( id, name terminated by "$", dept terminated by "^", salary, hiredon DATE "dd-mon-yyyy" )
For this example, let us use the following file which has data that are of
fixed length. For example, 1st three characters are always employee
number, Next 5 characters are always employee name, etc.
$ cat employee-fixed.txt
200JasonTechnology5500
300MaylaTechnology7000
400NishaTechnology9500
500RandyTechnology6000
Create the following control file, where you specific the position of each
and every field as shown below usig the Position(start:end) syntax.
$ cat sqlldr-fixed.ctl
load data
infile '/home/ramesh/employee-fixed.txt'
into table employee
Load this fixed length data using the sqlldr as shown below.
load data
infile '/home/ramesh/employee.txt'
into table employee
( id ":id+999",
name "upper(:name)",
dept "decode(:dept,'Technology','Techies', :dept)",
salary
Load the data using this control file which will massage the data before
uploading it.
Verify that the data got changed while loading as per our rules.
The following control file loads data from two different data files
(employee.txt and newemployee.txt) to the employee table.
$ sqlldr-add-multiple.ctl
load data
infile '/home/ramesh/employee.txt'
infile '/home/ramesh/newemployee.txt'
Load the data using this control file which will upload data from multiple
data files as shown below.
$ sqlldr scott/tiger control=/home/ramesh/sqlldr-add-multiple.ctl
bonus integer
);
Create the employee-bonus.txt data file that contains the fields: id, name,
department, salary, bonus
$ cat employee-bonus.txt
Create the control file as shown below, which will upload the data from the
above file to two different tables. As shown below, you should have two
into table commands, and specify the position of the data which needs to
be used to upload the data to that column.
$ cat sqlldr-multiple-tables.ctl
load data
infile '/home/ramesh/employee-bonus.txt'
( id position(1:3),
name position(5:10),
dept position(12:21),
salary position(23:26))
bonus position(28:31))
Load the data to multiple tables using this control file as shown below.
ID BONUS
---------- ----------
100 1000
200 2000
300 2000
400 1000
500 3000
$ cat employee-bad.txt
100,Thomas,Sales,5000
200,Jason,Technology,5500
300,Mayla,Technology,7K
400,Nisha,Marketing,9500
500,Randy,Technology,6K
$ cat sqlldr-bad.ctl
load data
infile '/home/ramesh/employee-bad.txt'
Load the data (including the invalid records) using this control file as shown
below.
As you see from the abvoe output, it still says logical record count 5, but
you should check the log files to see if it has rejected any records.
The log file indicates that 2 records are rejected as shown below:
Table EMPLOYEE:
By default the rejected records are stored in a file that has the same name
as the data file (but with .bad extension)
$ cat employee-bad.bad
300,Mayla,Technology,7K
500,Randy,Technology,6K
As you see below, the employee table has only 3 records (as 2 of them
were rejected).
Add the line when next to into table line. In the following control file,
the when clause indicates that it will load only the records that have dept
as Technology.
$ cat sqlldr-when.ctl
load data
infile '/home/ramesh/employee.txt'
As you see from the above output, it still says logical record count 5, but
you should check the log files to see how many records were loaded, and
how many records were discarded because it didnt match the when
condition.
The following from the log file shows that 5 records were read, and 2 of
them were discarded as it didnt match the when condition.
Verify that only the selective records were loaded into the table.
Nice
L I N K
hdaz
L I N K
Prasath
This is Nice.
Thanks.
L I N K
pathum
L I N K
L I N K
Manjula
L I N K
Prithviraj
@Manjula:
Ramesh has explained answer to your question in example 8.
L I N K
Prithviraj
Hello Ramesh,
Regarding disabling bad files i confirmed. In windows it can be done using:
BADFILE NUL
And in linux it can be done using: BADFILE /dev/null
L I N K
Rohit K
Hi Ramesh,
I need your help in loading the single file where the scenario quite clumsy
for me:
The data file sale_exec.dat:
_____________________________________________
CUST_TYPE | DATE | AMT | DISCOUNT_AMT
X |20120101 | 200 | 20
X |20120101 | 400 | 30
Y |20120303 | 300 |40
Z |20120303 | 20 |50
_______________________________________________
Scenario:
1. The data within the file sales_exec.dat has to be loaded into the table
SALES
2. When CUST_TYP is X the Vendorid should be 1 , when Y it should load
vendor ID with 2 and similarly when it is Z then Vendor ID should be 3
3. When the CUST_TYP is X and Y I need to load the records with the AMT in
the data file into the SALES table AMOUNT field
4. When the CUST_TYP is Z then I need to load the records with the
DISCOUNT_AMT in the data file into the SALES table AMOUNT FIELD
The table data from the Data file after load should look like below:
_____________________________________________
VENDORID | REPORT_DATE| AMOUNT
1 |20120101 | 200
1 |20120101 | 400
2 |20120303 | 300
3 |20120303 | 50
_______________________________________________
Please let me know how should I proceed further and what should be my
cntl file.
Thanks in advance!
Regards,
Rohit
L I N K
Prithviraj
@Rohit,
This is not possible using SQL loaders. Because data type of VendorId is
numeric and from infile you are getting characters. You have two
approches here:
1) Either you process your infile first and replace X by 1, y by 2, z by 3.
Then use SQL loader using when condition to check what vendoe id is it
and amount column should take what value. OR
2) You need to use external tables and SQL functions in this scenario.
Refer:
http://asktom.oracle.com/pls/asktom/f?
p=100:11:0::::P11_QUESTION_ID:1710164700346004127
L I N K
Rohit K
I tried using the boundfiller,decodes and my CONTROL file will look like this:
INFILE=sale_exec.dat
APPEND
PRESERVE BLANKS
INTO TABLE SALES
FIELDS TERMINATED BY , OPTIONALLY ENCLOSED BY TRAILING
NULLCOLS
(
c1 BOUNDFILLER,
REPORT_DATE,
c2 BOUNDFILLER,
c3 BOUNDFILLER,
VENDORID to_number(DECODE(:c1,X,1,Y,2,3)),
AMOUNT to_number(DECODE(:c1,Z,c3,c2))
)
Regards,
Rohit K
L I N K
SantoshCA
Hi Ramesh,
this is a very good writeup! Is there anything more advanced which
SQLLDR can handle? Would be great if you could write something on this
too.
L I N K
Jurgen
HI all,
I wanted too load mulitple files into the same tables from different ctl files
for test work. The problem is i need to be able too identify the different
files loaded in de database.
For example :
file1.ctl needs to be de name in de database under for example column
filename
file2.ctl needs to be de name in de database under for example column
filename
Kind regards,
Jurgen
L I N K
Mahes Tripathi
Thanks for giving such valuable examples. Could you please give
one example of control file to upload data in a file and then call a
procedure to implement some logic and populate main table.
L I N K
souji
L I N K
souji
Hi All,
above line, you can notice that there is no specified space between fields. i
have nearly 7000 rows of data in notepad. I tried using field terminated by
space but, it has taken the entire row of data from notepad as a single
column data in table, remaining fields in table are empty.
In this case is there any kind of solution/control file format to load the data
into tables. It would be great if anyone can solve my problem.
Thanks
souji
L I N K
sanchit
L I N K
Kenneth Y
L I N K
Dhawal Limbuwala
L I N K
ashok
L I N K
Imteyaz
L I N K
Naveen
Naresh
L I N K
Ashok
Nic explanation
L I N K
Praveen Kumar
Thanks.
The article is very good and easily understandable.
L I N K
praveen
L I N K
rajesh
L I N K
Muhd Islam
L I N K
Gauthama P
Thanks!
L I N K
charan
its very useful helpful for starters thanks for a very simple
explanation
L I N K
Shivanand
L I N K
Vivek V
L I N K
Aabid
L I N K
CRP
L I N K
Prasad
Is there any way to terminate the control file i mean to exit sqlldr and come
back to DOS prompt? It is not coming out of sqlldr mode but inserting
data is done perfectly.. any help appreciated
L I N K
Satya
L I N K
Uday
L I N K
Uday
I got it. Keeping FILLER keyword to the right of column name would
skip the column:
For e.g.:
OPTIONS (SKIP=1)
LOAD DATA
INFILE source.csv
BADFILE source.csv.bad
DISCARDFILE source.csv.dsc
APPEND
INTO TABLE tab_name
FIELDS TERMINATED BY , OPTIONALLY ENCLOSED BY
TRAILING NULLCOLS
( a FILLER
,c1
,c2
,c3
,c4
,c5
)
L I N K
Rajiv
Nice doc
L I N K
annuj
how to insert last n rows from a data file through control file in sql
loader?
L I N K
N S KRISHNA
Hi,
How to insert alternate rows into two different tables. I mean to insert
1,3,5,7,9, Records into Table1 and 2,4,6,8,10,.. into Table 2. Is there
any option to build control to achieve this? Please let me know.
Thank You
L I N K
vikram
L I N K
anudeep
nice article
L I N K
Prasanna Suri
L I N K
phani
L I N K
Tushar Sharma
Hi,
Thanks for great article, Is there any way to write control file with
update statements. I want to update few records. Is there any way around
?
Thanks
-Tushar
L I N K
M.Ayaz
L I N K
Mayur
L I N K
Pratibha
L I N K
Tushar
L I N K
Vinoth
L I N K
MANOJ REDDY
L I N K
Sudhakar
Hi All,
Thanks for the wonderful sharing. I am stuck here. hope someone can help
me.
I am trying to upload a flat file(824 rows) in an oracle db but i am getting
an error.
CONTROL FILE
====================
load data
infile /sit/bcpr/bcpdata/ETL/SUDS/hk_stg_finiq_cust_acct.txt
into table ABC123
fields terminated by , optionally enclosed by TRAILING NULLCOLS
(BUSINESS_DATE,
ACCT_NO,
ID_DOC_TYPE_PRIM,
ID_DOC_NO_PRIM,
ACCT_REL_TYP_PRIM,
ID_DOC_TYPE1,
ID_DOC_NO1,
ACCT_REL_TYP1,
ID_DOC_TYPE2,
ID_DOC_NO2,
ACCT_REL_TYP2,
ID_DOC_TYPE3,
ID_DOC_NO3,
ACCT_REL_TYP3,
ID_DOC_TYPE4,
ID_DOC_NO4,
ACCT_REL_TYP4,
ACCT_STATUS,
ACCT_OPEN_BRANCH_NO,
ARM_CODE,
AGRMT_DT,
ACCT_OPN_DT,
ACCT_CL_DT,
PRD_MAP,
COUNTRY_CD,
CREATE_DATE,
CREATE_USER,
SRC_STM_SHORT_NM)
ERROR
==========
SQL*Loader: Release 10.2.0.1.0 Production on Fri Jul 11 17:56:59 2014
siva
L I N K
shakeer
L I N K
Anonymous
L I N K
prawin
L I N K
SheFixesThings
Thank you! I was able to figure out a few things that I was missing
but need one more thing that Im surprised isnt mentioned here.
INFILE
L:\MyFolder\MySubFolder\IMPORT\AUTOLOAD\AUTOLOAD2014M\%getMyPa
rm%*.csv
the BAT file has this to build getMyParm up to the day of the month
SET YEAR=%DATE:~10,4%
SET MONTH=%DATE:~4,2%
SET DAY=%DATE:~7,2%
SET DASH=_
SET WILD=*.csv
SET
getMyParm=AUTOLOAD%YEAR%M14%MONTH%%DAY%%DASH%%WILD%
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
I would think it would recognize a * per other CMD forums I checked. I even
tried hardcoding the *.csv on the INFILE within the CTL but that returns the
same result and the same error.
Thank you
L I N K
Ramana
i have tow tables Stage1 and Stage2 how to load stage1 data into
Stage2 in sql loader.
pls help me any one.
L I N K
vikash kumar
while coding in shell script and need to run script in other ip. So,
we do ssh but i dont want to hard code password. So is there any
alternate way to do this in shell scripting
L I N K
Anonymous
Can not be more better than this article about SQL*Loader. I really
loved it..
L I N K
Anonymous
Thanks
L I N K
SHARATH
L I N K
Vivek
Awesome post!
L I N K
Lajpat Kurdiya
L I N K
Bhaskar Reddy
L I N K
Pravin Singh
prakash
You helped lot of people to understand what actually the sql loader
is and how it worksThanks from all of usKeep post your articels..
L I N K
Vaibhav
My column is shipped date and data type is date but at some place
it has written null thats why in my particular table data wasnt show
please help how can I overcome this.
L I N K
durga
Hi Ramesh,
As per my requirement Sql* loader should not enter a single record into
table if even one row contain wrong data.
L I N K
Anonymous
can we load the data from a single OS file into multiple tables
without using data positions
L I N K
ANKIT
L I N K
VIVEK
L I N K
Brindhavi
L I N K
mee
L I N K
Himanshu
L I N K
Leave a Comment
Name
Website
Comment
Submit
Notify me of followup comments via e-mail
Next post: 10 Linux DU Command Examples (Files and Folders Disk Usage
Size)
Previous post: How to Compile and Install Software from Source Code on
Linux
eplaque.fr
yakarouler.com
capvacances.fr
E B O O K S
Linux 101 Hacks 2nd Edition eBook - Practical Examples to Build a Strong Foundation
in Linux
Bash 101 Hacks eBook - Take Control of Your Bash Command Line and Shell Scripting
Sed and Awk 101 Hacks eBook - Enhance Your UNIX / Linux Life with Sed and Awk
Vim 101 Hacks eBook - Practical Examples for Becoming Fast and Productive in Vim
Editor
P O P U L A R P O S T S
12 Amazing and Essential Linux Books To Enrich Your Brain and Library
3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id
C A T E G O R I E S
Linux Tutorials
Vim Editor
Sed Scripting
Awk Scripting
Nagios Monitoring
OpenSSH
IPTables Firewall
MySQL Database
Perl Programming
Google Tutorials
Ubuntu Tutorials
PostgreSQL DB
Hello World Examples
C Programming
C++ Programming
Oracle Database
VMware Tutorials
Ramesh Natarajan
Suivre
A B O U T T H E G E E K S T U F F
C O N T A C T U S
Email Me : Use this Contact Form to get in touch me with your comments, questions
or suggestions about this site. You can also simply drop me a line to say hello!.
Follow us on Google+
Follow us on Twitter
S U P P O R T U S