EmpId Name
----------------------
E0008 Mary _Jane
EMP0009 PeterParker
10 Jack roger
EMP 0012 Mark Todd
EMP0011 John Smith
EMP13 *Peter Parker
Need to satisfy below conditions
Load data from this file (load as EXCEL or CSV)
While loading data create multiple batches
While loading data, apply below rules
My control is below
load data
infile 'E:\A\Book2.csv'
TRUNCATE
PRESERVE BLANKS
into table dummy
fields terminated by ','
optionally enclosed by '"'AND'"'
(
EMPID "CASE WHEN SUBSTR(:EMPID,1,1)='E' THEN 'EMP00'||SUBSTR(:EMPID,-2) ELSE
'EMP00'||SUBSTR(:EMPID,-2) END",
NAME "INITCAP(REGEXP_REPLACE(:NAME,'[^A-Z a-z]'))"
)
I'm unable to satisfy conditions 2, 3b, 3d
Have a look at the following example.
Control file:
load data
infile *
replace
into table dummy
fields terminated by ";"
trailing nullcols
(
empid "'EMP' || lpad(regexp_substr(:empid, '[[:digit:]]+$'), 4, '0')",
name "trim(initcap(regexp_replace(regexp_replace(regexp_replace(:name, '[^[:alnum:] ]'), '([[:upper:]])', ' \\1'), ' +', ' ')))"
)
begindata
E0008;Mary _Jane
EMP0009;PeterParker
10;Jack roger
EMP 0012;Mark Todd
EMP0011;John Smith
EMP13;*Peter Parker
Loading session and the result:
SQL> $sqlldr scott/tiger@kc11gt control=test32.ctl log=test32.log
SQL*Loader: Release 11.2.0.1.0 - Production on Uto Ruj 1 10:18:01 2020
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 5
Commit point reached - logical record count 6
SQL> select * from dummy;
EMPID NAME
---------- --------------------
EMP0008 Mary Jane
EMP0009 Peter Parker
EMP0010 Jack Roger
EMP0012 Mark Todd
EMP0011 John Smith
EMP0013 Peter Parker
6 rows selected.
SQL>
What does the control file do?
For EMPID
: as it must begin with EMP
, it is set to be a constant. Digits within input data are left padded with zeros up to 4 characters in length.
For NAME
:
[^[:alnum:] ]
removes anything but alphanumerics and spaces'([[:upper:]])', ' \\1'
adds space in front of every uppercase letter' +', ' '
replaces multiple spaces with a single spaceINITCAP
capitalizes first letters in every wordTRIM
removes leading (and trailing) spaces