Home » RDBMS Server » Server Utilities » How to use a variable in a control file
How to use a variable in a control file [message #73218] Wed, 03 March 2004 20:41 Go to next message
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 "&#124"
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 #73229 is a reply to message #73223] Fri, 05 March 2004 05:02 Go to previous messageGo to next message
d
Messages: 18
Registered: October 2000
Junior Member
s
Re: How to use a variable in a control file [message #73230 is a reply to message #73223] Fri, 05 March 2004 05:12 Go to previous messageGo to next message
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

Re: How to use a variable in a control file [message #73236 is a reply to message #73223] Sat, 06 March 2004 04:10 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10708
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
###### first a demo ########
#
#### if give no input values...nothng happens.
#
C:>member
#
#######infile is me.dat sqlldr session is in append mode
#
C:>member me.dat

SQL*Loader: Release 8.1.6.0.0 - Production on Fri Mar 5 16:14:10 2004

(c) Copyright 1999 Oracle Corporation.  All rights reserved.

Commit point reached - logical record count 3

#
#######infile is abc.txt. sqlldr session is in append mode
#
C:>member abc.txt

SQL*Loader: Release 8.1.6.0.0 - Production on Fri Mar 5 16:14:28 2004

(c) Copyright 1999 Oracle Corporation.  All rights reserved.

Commit point reached - logical record count 3

#
##lets check whether the data is been inserted
# 

C:>sqlplus -s mag/mag@mutation
select * from member;

NAME       FILE_ID    ADDRESS
---------- ---------- ----------
Charlie    me.dat     addres1
Peter      me.dat     addres2
Ben        me.dat      address2
name1      abc.txt    addres11
name2      abc.txt    addres21
name3      abc.txt     address21
name1      abc.txt    addres11
name2      abc.txt    addres21
name3      abc.txt     address21

9 rows selected.

exit
#
#####this is batch file which creates the member.ctl
#
C:>cat member.bat
@echo off
:loop
if "%1"=="" GOTO continue
       echo load data                          >member.ctl
       echo infile '%1'                        >>member.ctl
       echo append into table member           >>member.ctl
       echo fields terminated by ','           >>member.ctl
       echo (                                  >>member.ctl
       echo name,                              >>member.ctl
       echo file_id constant '%1',     >>member.ctl
       echo address                            >>member.ctl
       echo )                                  >>member.ctl
       sqlldr userid=mag/mag@mutation control=member.ctl
       :End
SHIFT
GOTO Loop
:continue

C:>cat member.ctl
load data
infile 'abc.txt'
append into table member
fields terminated by ','
(
name,
file_id constant 'abc.txt',
address
)

Re: How to use a variable in a control file [message #73257 is a reply to message #73236] Wed, 10 March 2004 00:01 Go to previous message
sri
Messages: 154
Registered: February 2000
Senior Member
Thanks a lot for the answer!!!
Previous Topic: Sql Loader Problem
Next Topic: Insert into multiple tables
Goto Forum:
  


Current Time: Thu Nov 21 23:26:22 CST 2024