Hello! SQL Loader is one of those tools that boast very high speeds for loading data into the database. Sometimes, it feels like it memorizes part of the data! 😄
Here's the test file we'll be working with today:
To use SQL Loader, we need a control file that specifies the table structure and how to map the data:
[oracle@db11g-node2 ~]$ cat sqlldr.ctl
OPTIONS
(
SKIP=1,
PARALLEL=true,
DIRECT=true,
SKIP_INDEX_MAINTENANCE=true
)
LOAD DATA
APPEND
INTO TABLE VAHID_TEST_SQLLDR
FIELDS
TERMINATED BY ","
TRAILING
NULLCOLS
(
ID sequence,
DATE date 'yyyy/mm/dd-hh24:mi:ss',
NUMBER_TEST,
PHONE,
TEXT_TEST,
TEXT_CONSTANT constant 'vahidnowrouzi.blog.ir'
)
Key Points in the Control File:
-
Auto-Generating Sequence Values: Initially, I tried using
sequence1.nextval
for theID
field but encountered issues. Eventually, I found that simply using the keywordsequence
allows SQL Loader to handle this automatically. You can find more details here. -
Date Format Handling: The control file includes the date format specification to correctly parse and insert date values into a
DATE
field. -
Adding Constant Values: The control file also demonstrates how to add constant values for specific fields, as shown with
TEXT_CONSTANT
.
Table Creation
The table to hold the imported data is created as follows:
CREATE TABLE VAHID_TEST_SQLLDR
( "ID" NUMBER(20,0),
"DATE" DATE,
"NUMBER_TEST" NUMBER(3,0),
"PHONE" VARCHAR2(20 BYTE),
"TEXT_TEST" VARCHAR2(50),
"TEXT_CONSTANT" VARCHAR2(50)
);
Running SQL Loader
Execute the SQL Loader command as follows:
sqlldr vahid/vahid data=/home/oracle/sqlldr.data control=/home/oracle/sqlldr.ctl log=/home/oracle/sqlldr.log bad=/home/oracle/sqlldr.bad
The log
and bad
parameters specify files that will be created to log the process and capture any problematic records, respectively.
This ensures that any issues can be tracked and resolved based on these log files.