How to use a variable in a control file [message #73218] |
Wed, 03 March 2004 20:41 |
sri
Messages: 154 Registered: February 2000
|
Senior Member |
|
|
Hi,
I am new to control files but I have a question which is bothering me for long. I have a control file like
1 LOAD DATA
2 INFILE '%FILE_PATH%Feedabc.txt'
3 insert INTO TABLE mytable
4 fields terminated by "|"
5 trailing nullcols
6 ( NAME CHAR,
7 seqno "citicdms_seqno.nextval",
8 fileid "decode(:fileid,null,cliendfeed_FileId(4,'abc.txt'))"
9 )
here %FILE_PATH% is a enviornment variable which i am using to store the path. Similarly i want the text file to be stored in a variable which I should be able to use it in line#2 and line#8.
I am calling this ctl file from a .bat file. I want to pass the name of the file on command line and which i can use in the ctl file. Please guide.
Regds, Sri
|
|
|
Re: How to use a variable in a control file [message #73220 is a reply to message #73218] |
Thu, 04 March 2004 05:04 |
|
Mahesh Rajendran
Messages: 10708 Registered: March 2002 Location: oracleDocoVille
|
Senior Member Account Moderator |
|
|
You have to write a wrapper script that dynaamically builds a control file.
depending upon your OS and available scripting tools, you can use anything
method u want ( perl/sqlplus/python/C/shell/dosshell....)
this is the basic idea....
############sample session#######################################################
# batch file = member
# input filename to be loaded = member.dat
# there two files. member.ctl.base which is like a template with a variable $INPUT
# every time u pass a new file name,a new member.ctl is created,
# which is used inside the sql*loader.
#################################################################################
bash-2.03$ member member.dat
this is the new control file to be used by sqlloader
load data
infile 'member.dat'
insert into table member
fields terminated by ',' trailing nullcols
(
name,
address
)
SQL*Loader: Release 9.2.0.1.0 - Production on Thu Mar 4 09:57:11 2004
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Commit point reached - logical record count 5
#######lets try another filename
#######first lets remove the old records
bash-2.03$ sqlplus -s mag/mag << EOF
> delete from member;
> commit;
> exit
> EOF
3 rows deleted.
Commit complete.
###try again with new filename
bash-2.03$ member member2.dat
this is the new control file to be used by sqlloader
load data
infile 'member2.dat'
insert into table member
fields terminated by ',' trailing nullcols
(
name,
address
)
SQL*Loader: Release 9.2.0.1.0 - Production on Thu Mar 4 09:57:32 2004
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Commit point reached - logical record count 5
##############these are scripts i used#############
###
bash-2.03$ cat member
#!/usr/bin/bash
cat member.ctl.base | sed -e "s%$INPUT%'$1'%" > member.ctl
echo
echo this is the new control file to be used by sqlloader
echo
cat member.ctl
sqlldr userid=mag/mag control=member.ctl
#####base control file used as template...############
bash-2.03$ cat member.ctl.base
load data
infile $INPUT
insert into table member
fields terminated by ',' trailing nullcols
(
name,
address
)
|
|
|
Re: How to use a variable in a control file [message #73223 is a reply to message #73220] |
Thu, 04 March 2004 21:44 |
sri
Messages: 154 Registered: February 2000
|
Senior Member |
|
|
Hi,
Thanks a lot for the prompt reply. It was nice to get a solution but the answer couldn't solve my second question. How to assign this variable in one of the columns of the table. Suppose i want to store the name of the file in one of the tables.
Moreover I am not that good in unix but somehow i could make out what you meant. But it would be better and helpful if I get the dos code :). sorry for asking more.
Regards,
Sri
|
|
|
|
Re: How to use a variable in a control file [message #73230 is a reply to message #73223] |
Fri, 05 March 2004 05:12 |
|
Mahesh Rajendran
Messages: 10708 Registered: March 2002 Location: oracleDocoVille
|
Senior Member Account Moderator |
|
|
pretty much the same concept.
you have to dynamically create the control file.
if you are not comfortable with unix,
try perl or just sqlplus!! ( i agree, it is not very amusing to do it in sqlplus...but...it works)!.
PLEASE FOLLOW UP WITH THE URLS (our previous postings) GIVEN BELOW FOR SAMPLE SESSIONS.
It is almost your case.
Idea is to include key word constant in your control files.
load data
..
..
(
col1,
filename constant $input,
col3
)
...
example using sqlplus
http://www.orafaq.com/forum/t/26637/0/
and also follow up with this ( the syntax in script would be changed becuase of << tags)
http://www.orafaq.com/forum/t/26637/0/
for unix example
http://www.orafaq.com/forum/t/26637/0/
If you can followup with all the postings that same thread, everything is explained...(how the script works etc).
the posted sessions are almost same as yours...only differece is , the constant to be inserted is fetched from a database sequence...
WINDOWS scripting is really not my domain.
But still you can use perl?
anyhow i will try to create something in windows....
[Updated on: Fri, 18 February 2005 23:32] Report message to a moderator
|
|
|
|
|