Solved

How to pass variables into SQL loader control file?

Posted on 2004-04-15
12
27,148 Views
Last Modified: 2007-12-19
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

0
Comment
Question by:vemul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 13

Expert Comment

by:riazpk
ID: 10832721
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
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 10832737
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
 
LVL 5

Author Comment

by:vemul
ID: 10833443
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 10833624
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
 
LVL 6

Expert Comment

by:musdu
ID: 10835324
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
 
LVL 3

Expert Comment

by:rajnadimpalli
ID: 10847453
@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
 
LVL 5

Author Comment

by:vemul
ID: 10848975
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
 
LVL 5

Author Comment

by:vemul
ID: 10873084
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
 
LVL 8

Expert Comment

by:Pierrick LOUBIER
ID: 11486317
0
 
LVL 8

Accepted Solution

by:
Pierrick LOUBIER earned 50 total points
ID: 11506209
Answer accepted for http://www.experts-exchange.com/Databases/Oracle/Q_21049777.html

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question