Solved

How to pass variables into SQL loader control file?

Posted on 2004-04-15
12
26,046 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
  • 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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now