Solved

How to pass variables into SQL loader control file?

Posted on 2004-04-15
12
25,747 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
Comment Utility
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 34

Expert Comment

by:Mark Geerlings
Comment Utility
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
Comment Utility
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 34

Expert Comment

by:Mark Geerlings
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 8

Accepted Solution

by:
Pierrick LOUBIER earned 50 total points
Comment Utility
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

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.

Join & Write a Comment

Suggested Solutions

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Via a live example, show how to take different types of Oracle backups using RMAN.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

771 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

10 Experts available now in Live!

Get 1:1 Help Now