We have used SQL*Loader and External Tables to load the data from flat files into database tables, however for SQL*Loader we need to write a control file
using which data is loaded and for External tables we need to write a script and syntax for the same is not quite straight forward. With Oracle 12c, there
is new mode of SQL*Loader called as Express Mode.
Using Express Mode is quite straight forward with a simple command line interface, and this can quite useful when you have to load data from plain CSV files.
The main benefit of SQL*Loader Express Mode is savings of time and effort that are saved from not needing to write a control file. Instead we just specify
a SQL*Loader command with few required parameters and the load starts.
Another benefit of SQL*Loader express mode is that it will try use the fastest mechanism for loading data files
1. External tables using parallel inserts with append hint
2. If External tables can not be used for a particular load operation, then it will use direct path load in SQL*Loader
In Oracle 12c we can turn on the express mode of SQL*Loader by speifying TABLE parameter in the command. The value of the TABLE parameter is name of the
table in which data will be populated. If we only specify the TABLE parameter then SQL*Loader will do the following:
1. It will look for a file with same name as TABLE value ending with “.dat” in the current directory.
2. It also assumes the order of values in data files matches as that of order of columns in table.
3. The fields are terminated by commas.
For our example, we create the following table which will store information about routes:
CDB$ROOT@ORCL> create table route(
routeid number(2)
, routename varchar2(30)
, startpt varchar2(30)
, endpt varchar2(30)
); 2 3 4 5 6
Table created.
Now we have a CSV file “route.dat” with following contents:
10, Route1, Huda City Centre, Badshahpur
20, Route2, Bus Stand, Iffco Chowk
30, Route3, Bus Stand, Subhash Chowk
Now to load it its contents into our table we just simply issue the following command
[oracle@localhost jagdeep]$ sqlldr c##jagdeep/jagdeep#1234@orcl TABLE=route
SQL*Loader: Release 12.1.0.1.0 - Production on Mon Aug 29 02:43:02 2016
Copyright (c) 1982, 2013, Oracle and/or its affiliates. All rights reserved.
Express Mode Load, Table: ROUTE
Path used: External Table, DEGREE_OF_PARALLELISM=AUTO
Table ROUTE:
3 Rows successfully loaded.
Check the log files:
route.log
route_%p.log_xt
for more information about the load.
As from the above, we can look into route.log for information
SQL*Loader: Release 12.1.0.1.0 - Production on Mon Aug 29 02:43:02 2016
Copyright (c) 1982, 2013, Oracle and/or its affiliates. All rights reserved.
Express Mode Load, Table: ROUTE
Data File: route.dat
Bad File: route_%p.bad
Discard File: none specified
(Allow all discards)
Number to load: ALL
Number to skip: 0
Errors allowed: 50
Continuation: none specified
Path used: External Table
Table ROUTE, loaded from every logical record.
Insert option in effect for this table: APPEND
Column Name Position Len Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
ROUTEID FIRST * , CHARACTER
ROUTENAME NEXT * , CHARACTER
STARTPT NEXT * , CHARACTER
ENDPT NEXT * , CHARACTER
Generated control file for possible reuse:
OPTIONS(EXTERNAL_TABLE=EXECUTE, TRIM=LRTRIM)
LOAD DATA
INFILE 'route'
APPEND
INTO TABLE ROUTE
FIELDS TERMINATED BY ","
(
ROUTEID,
ROUTENAME,
STARTPT,
ENDPT
)
End of generated control file for possible reuse.
created temporary directory object SYS_SQLLDR_XT_TMPDIR_00000 for path /u01/oracle/jagdeep
enable parallel DML: ALTER SESSION ENABLE PARALLEL DML
creating external table "SYS_SQLLDR_X_EXT_ROUTE"
CREATE TABLE "SYS_SQLLDR_X_EXT_ROUTE"
(
"ROUTEID" NUMBER(2),
"ROUTENAME" VARCHAR2(30),
"STARTPT" VARCHAR2(30),
"ENDPT" VARCHAR2(30)
)
ORGANIZATION external
(
TYPE oracle_loader
DEFAULT DIRECTORY SYS_SQLLDR_XT_TMPDIR_00000
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE CHARACTERSET US7ASCII
BADFILE 'SYS_SQLLDR_XT_TMPDIR_00000':'route_%p.bad'
LOGFILE 'route_%p.log_xt'
READSIZE 1048576
FIELDS TERMINATED BY "," LRTRIM
REJECT ROWS WITH ALL NULL FIELDS
(
"ROUTEID" CHAR(255),
"ROUTENAME" CHAR(255),
"STARTPT" CHAR(255),
"ENDPT" CHAR(255)
)
)
location
(
'route.dat'
)
)REJECT LIMIT UNLIMITED
executing INSERT statement to load database table ROUTE
INSERT /*+ append parallel(auto) */ INTO ROUTE
(
ROUTEID,
ROUTENAME,
STARTPT,
ENDPT
)
SELECT
"ROUTEID",
"ROUTENAME",
"STARTPT",
"ENDPT"
FROM "SYS_SQLLDR_X_EXT_ROUTE"
dropping external table "SYS_SQLLDR_X_EXT_ROUTE"
Table ROUTE:
3 Rows successfully loaded.
Run began on Mon Aug 29 02:43:02 2016
Run ended on Mon Aug 29 02:43:13 2016
Elapsed time was: 00:00:10.13
CPU time was: 00:00:00.03
So now our table is populated with these three records from the CSV file. We can confirm the same as following:
CDB$ROOT@ORCL> select * from route;
ROUTEID ROUTENAME STARTPT ENDPT
---------- ------------------------------ ------------------------------ ------------------------------
10 Route1 Huda City Centre Badshahpur
20 Route2 Bus Stand Iffco Chowk
30 Route3 Bus Stand Subhash Chowk
Changing Field Terminator:
We can also change the field terminator from the default “,” to any other value, for example lets change the terminator to “:” as following in the file
40: Route1: Huda City Centre: Badshahpur
50: Route2: Bus Stand: Iffco Chowk
And now run our load with the following command:
[oracle@localhost jagdeep]$ sqlldr c##jagdeep/jagdeep#1234@orcl TABLE=route terminated_by=':'
SQL*Loader: Release 12.1.0.1.0 - Production on Mon Aug 29 03:00:22 2016
Copyright (c) 1982, 2013, Oracle and/or its affiliates. All rights reserved.
Express Mode Load, Table: ROUTE
Path used: External Table, DEGREE_OF_PARALLELISM=AUTO
Table ROUTE:
2 Rows successfully loaded.
Check the log files:
route.log
route_%p.log_xt
for more information about the load.
And check from the table about these values
CDB$ROOT@ORCL> select * from route;
ROUTEID ROUTENAME STARTPT ENDPT
---------- ------------------------------ ------------------------------ ------------------------------
10 Route1 Huda City Centre Badshahpur
20 Route2 Bus Stand Iffco Chowk
30 Route3 Bus Stand Subhash Chowk
40 Route1 Huda City Centre Badshahpur
50 Route2 Bus Stand Iffco Chowk
Loading from multiple files:
We can also load data from multiple files in single command. For example we having following two files “GgnCity.dat” and “DelCity.dat”:
"GgnCity.dat"
10, Route1, Huda City Centre, Badshahpur
20, Route2, Bus Stand, Iffco Chowk
30, Route3, Bus Stand, Subhash Chowk
"DelCity.dat"
40, Route1, Rajiv Chowk, ISBT
50, Route2, Dhaula Kuan, Rohini
And we specify the following command for loading data
[oracle@localhost jagdeep]$ sqlldr c##jagdeep/jagdeep#1234@orcl TABLE=route data=GgnCity.dat,DelCity.dat
SQL*Loader: Release 12.1.0.1.0 - Production on Mon Aug 29 03:10:53 2016
Copyright (c) 1982, 2013, Oracle and/or its affiliates. All rights reserved.
Express Mode Load, Table: ROUTE
Path used: External Table, DEGREE_OF_PARALLELISM=AUTO
Table ROUTE:
5 Rows successfully loaded.
Check the log files:
route.log
route_%p.log_xt
for more information about the load.
And check from the database for our current values:
CDB$ROOT@ORCL> select * from route;
ROUTEID ROUTENAME STARTPT ENDPT
---------- ------------------------------ ------------------------------ ------------------------------
10 Route1 Huda City Centre Badshahpur
20 Route2 Bus Stand Iffco Chowk
30 Route3 Bus Stand Subhash Chowk
40 Route1 Rajiv Chowk ISBT
50 Route2 Dhaula Kuan Rohini
We can also use wildcard characters while specifying the name of the files as following
[oracle@localhost jagdeep]$ sqlldr c##jagdeep/jagdeep#1234@orcl TABLE=route data=*City.dat
Specifying the fields names in the data file:
We can also specify the field names in the CSV file itself and they need not to be in the same order as of the table fields.
startpt, endpt, routeid, routename
Huda City Centre, Badshahpur, 10, Route1
Bus Stand, Iffco Chowk, 20, Route2
Bus Stand, Subhash Chowk, 30, Route3
And use the parameter FIELD_NAMES as following to load the data:
[oracle@localhost jagdeep]$ sqlldr c##jagdeep/jagdeep#1234@orcl TABLE=route FIELD_NAMES=first
SQL*Loader: Release 12.1.0.1.0 - Production on Mon Aug 29 04:02:38 2016
Copyright (c) 1982, 2013, Oracle and/or its affiliates. All rights reserved.
Express Mode Load, Table: ROUTE
Path used: External Table, DEGREE_OF_PARALLELISM=AUTO
Table ROUTE:
3 Rows successfully loaded.
Check the log files:
route.log
route_%p.log_xt
for more information about the load.
Summary
SQL*Loader Express Mode greatly simpifies the work needed to load simple CSV data files. Express mode tries to use the fastest mechanism
to load the data and provides additional parameters which can be used to customized our load.
Hope this helps:
Cheers!!!
Jagdeep Sangwan