How to pass variables into SQL loader control file?

Hello,
 I have a control file test.ctl like this:

LOAD DATA
INSERT NTO TABLE TEST
FIELDS TERMINATED BY '|'
(
COL1,
COL2,
COL3   "1234"
)

and I load the data by

sqloader control = test.ctl data=test.data ...

I have to keep changing the value for COL3 based on the requirements, so I would like to pass this value from outside instead of always modifying the control file. Is that possible?

Thanks,
vemul

LVL 5
vemulAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

riazpkCommented:
Thats simple...you will use before insert trigger for each row on table test and code like this:

if <Your conditions go here> then
:new.col3=YourValue
else
......
....
..

end if;

No need to mention it in the control file.

Alternatively if you have defined some procedure (in which you do some processing and return value for col3) then u can do something like:


LOAD DATA
INSERT NTO TABLE TEST
FIELDS TERMINATED BY '|'
(
COL1,
COL2,
COL3   "ProcedureName(Param1,Param2,....)"
)

I am quite doubtfull about my 2nd option ...you will have to try it out.

0
Mark GeerlingsDatabase AdministratorCommented:
No, not directly, since SQL*loader and the control files for SQL*Loader do not accept parameters.

Now that is likely not the answer you were hoping for, but there are some alternatives.

One option is to fill this third column with a pre-insert trigger on the table instead of filling it via SQL*Loader.  Then you could adjust the trigger when necessary and not have to change the control file.  This may not be a huge improvement, since it still requires a DDL statement.

A variation of this would involve writing a PL\SQL function that the pre-insert trigger could call to get the value.  This function could select a value from a table, so you only have to add or modify a record in a control table before each run.
0
vemulAuthor Commented:
Hi riazpk and markgeer,
 Thank you for your comments. The problem is that I do not have permissions to modify the DDL. Also, I have to keep changing the value every week. So I don't see any benefit out of this. I was wondering if there was any way one could simply pass variables as command line parameters (like the one we can do with sqlplus.  I can't understand why they wouldn't allow the same functionality with sqlloader also). I have searched everywhere and couldnt find any solution.

Thanks,
vemul
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Mark GeerlingsDatabase AdministratorCommented:
It may be possible to call a PL\SQL function in the control file, something like the syntax that riazpk suggested with a procedure.  I am quite sure that a procedure will not work that way, but a function may (I haven't tried).  If so, this would give you a way to supply a different value with DDL changes each time.  You would just need a one-time DDL command to create the function and a table that the function could select the value from.
0
musduCommented:
Hi,

write a batch file and pass this parameter to batch file. Batch file should first create sqlloader control file (test.ctl) with desired parameter and then start the sqlloader.

this is an example of ms-dos batch (unix shell script will be similar)

loadtest.bat
------------------------
Set myctl=test.ctl

echo LOAD DATA                  > %myctl%
echo INSERT NTO TABLE TEST      >> %myctl%
echo FIELDS TERMINATED BY '|'   >> %myctl%
echo (                          >> %myctl%
echo COL1,                      >> %myctl%
echo COL2,                      >> %myctl%
echo COL3   "%1"                >> %myctl%
echo )                          >> %myctl%

rem ** call sqlloader **

sqloader control = test.ctl data=test.data ...

you can execute your batch file like below;

loadtest.bat 1234

regards.
0
rajnadimpalliCommented:
@vemul : You should able to do...

$sqlldr userid=scott/tiger data=test.dat \
control=`sed 's/<Variable_name>/<New_vale>/g' test_tempate.ctl > test.ctl ; echo test.ctl `

Check-out below for demo ...

SQL> desc test
 Name              Null?    Type
 ----------------- -------- ------------
 COL1                       NUMBER
 COL2                       NUMBER
 COL3                       NUMBER

SQL>
SQL> select count(*) from test;

  COUNT(*)
----------
         0

SQL>!
$ cat test.dat
100|555
200|999
300|444

$ cat test_template.ctl
LOAD DATA
APPEND INTO TABLE TEST
FIELDS TERMINATED BY '|'  trailing NULLCOLS
(
COL1,
COL2,
COL3   "COL3_VALUE"
)
$

$ sqlldr userid=scott/tiger \
> data=test.dat \
> control=`sed 's/COL3_VALUE/1234/g' test_template.ctl >  /tmp/test.ctl; echo "/tmp/test.ctl"`

SQL*Loader: Release 9.2.0.1.0 - Production on Fri Apr 16 7:01:34 2004

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.

Commit point reached - logical record count 3

$ cat /tmp/test.ctl
LOAD DATA
APPEND INTO TABLE TEST
FIELDS TERMINATED BY '|' trailing NULLCOLS
(
COL1,
COL2,
COL3   "1234"
)

$

SQL> select * from test;

      COL1       COL2       COL3
---------- ---------- ----------
       100        555       1234
       200        999       1234
       300        444       1234

SQL>

 $ sed 's/COL3_VALUE/1234/g' test_template.ctl
LOAD DATA
APPEND INTO TABLE TEST
FIELDS TERMINATED BY '|' trailing NULLCOLS
(
COL1,
COL2,
COL3   "1234"
)
$

-R

0
vemulAuthor Commented:
Hi rajnadimpalli,
 Your suggestion is something what I was really looking for. Let me try it out over the week and shall let you know.

musdu, I liked even your idea though it did appear a bit strange initially. Shall try this out too and let you know.

Thanks,
vemul
0
vemulAuthor Commented:
Hi,
 I am pretty close to achieving what I wanted but am kind of stuck here. I wrote a sample shell script:

test.ksh

#!/bin/ksh
OUTFILE="output.txt"
echo > $OUTFILE

while read LINE
do
 echo ${LINE} >> $OUTFILE
done < test.txt
exit $?
----------------------------------------------------------
and the test.txt is

OPTIONS (SKIP_UNUSABLE_INDEXES=YES)
LOAD DATA APPEND
INTO TABLE OSE_RPST_TEMP
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
(
COL1                          FILLER,
COL2                          DATE  'MMDDYYYYHH24MISS',
COL3                          "$1"
)
----------------------------------------------------------------
it is the value of COL3 that I want to give as input parameter but when I run

./test.ksh VAL3

it does not replace the $1 value. Can you please tell me what am I doing wrong?

Thanks,
vemul

0
Pierrick LOUBIERIS Operational Excellence ManagerCommented:
0
Pierrick LOUBIERIS Operational Excellence ManagerCommented:
Answer accepted for http://www.experts-exchange.com/Databases/Oracle/Q_21049777.html

"You could also generate the CTL file with UTL_FILE..."
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.