Skip to Main Content
  • Questions
  • Specify file delimiter in sql loader command

Breadcrumb

Connor and Chris will both be at AI World from October 12 to October 17 , the premier Oracle conference of 2025. If you're in Vegas, please come say Hi or pop into our sessions

Question and Answer

Chris Saxon

Thanks for the question, Miram.

Asked: April 28, 2016 - 9:11 am UTC

Last updated: April 28, 2016 - 3:18 pm UTC

Version: 11.2.0.3

Viewed 10K+ times! This question is

You Asked

Hello,

I have a ksh file that calls sqlldr to load a file into a staging table.
I need to be able to send the delimiter (which is pipe in this case) as a parameter in the sqlldr command to the control file.
So in other words, I don't want the delimiter to be hardcoded in the control file (FIELDS TERMINATED BY '|'), instead I need it to be defined as a variable in the control file and specified as a parameter in the ksh file.

Is that possible?
Your help is much appreciated.

Sample ksh file:

#!/bin/ksh
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
export ORACLE_BIN=$ORACLE_HOME/bin
export DBCONN=username/password

$ORACLE_BIN/sqlldr $DBCONN data=filename.dat control=filename.ctl log=filename.log skip=1 direct=true

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sample Control file:

load data
truncate
into table stg_table
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
(field1 "trim( both '\"' from :field1)",
field2 "trim( both '\"' from :field2)",
field3 "trim( both '\"' from :field3)"
)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sample data file (filename.dat):

"Column1"|"Column2"|"Column3"
"value1"|"value2"|"value3"


and Connor said...

I would build a "template" for your controlfile, for example:

load data
truncate
into table stg_table
FIELDS TERMINATED BY '@@@@@' TRAILING NULLCOLS
(field1 "trim( both '\"' from :field1)",
field2 "trim( both '\"' from :field2)",
field3 "trim( both '\"' from :field3)"
)

So assuming that file is called: template.ctl, and the delimiter is stored in an environment variable calle: $DELIM

Then at run time, you would do this:

sed "s/@@@@@/$DELIM/g" template.ctl > control.ctl
sqlldr control=control.ctl ...

Hope this helps.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Miram Atie, April 28, 2016 - 4:51 pm UTC


More to Explore

Backup/Recovery

Check out the complete guide to all of the Backup & Recovery techniques in the Oracle Database.