sqlloader control file [message #35303] |
Fri, 04 February 2005 07:58 |
Ant
Messages: 13 Registered: February 2000
|
Junior Member |
|
|
The external files have the name convention: TTX[[1-10]].txt , total of 10 files to be loaded.
I built a control file:
load data
infile *
BADFILE 't.bad'
APPEND
into table Trans
fields terminated by ',' optionally enclosed by '"'
(
col1,
F1,
F2,
F3 filler,
F4,
F5,
F6
F7 filler
)
How do I insert col1 value with the file name convention
eg: if TTX3.txt file has 100 records to be loaded into db, all these 100 data records would have col1 value with TTX3.
Please help.
|
|
|
|
|
Re: sqlloader control file [message #35341 is a reply to message #35320] |
Mon, 07 February 2005 06:24 |
|
Mahesh Rajendran
Messages: 10708 Registered: March 2002 Location: oracleDocoVille
|
Senior Member Account Moderator |
|
|
use a little scripting.
use whatever you like
sed/perl/bash/korn/sqlplus/... whatever that works for you.
--
-- let this be sample table.
--
bash-2.03$ desc mutation mag.sample
Table:mag.sample
Name Null? Type
----------------------------------- -------- ------------------------
FILENAME VARCHAR2(10)
NAME VARCHAR2(10)
ADDRESS VARCHAR2(10)
--
-- these are our data files
--
bash-2.03$ cat file1.dat
Dave,2 main st
Bob,3 cross st
bash-2.03$ cat file2.dat
otherDave,2 main st
otherBob,3 cross st
--
-- lets load those files.
-- here 'sample' is the script that takes one parameter ( name of the file)
-- loading file1.dat
bash-2.03$ sample file1.dat
SQL*Loader: Release 9.2.0.4.0 - Production on Mon Feb 7 10:11:44 2005
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Commit point reached - logical record count 2
--
-- loading file2.dat
--
bash-2.03$ sample file2.dat
SQL*Loader: Release 9.2.0.4.0 - Production on Mon Feb 7 10:11:50 2005
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Commit point reached - logical record count 2
--
-- checking the data
--
bash-2.03$ query mutation mag.sample
FILENAME NAME ADDRESS
---------- ---------- ----------
file1.dat Dave 2 main st
file1.dat Bob 3 cross st
file2.dat otherDave 2 main st
file2.dat otherBob 3 cross st
--
-- this is the script
-- have a standard control file template.ctl
-- for every load, Edit the template and create a new control file.
-- use that new controlfile to load.
-- make use of constant cluase in sql*loader.
--
bash-2.03$ cat sample
#!/usr/bin/bash
export ORACLE_HOME=/u01/app/oracle/product/9.2.0
export PATH=$PATH:$ORACLE_HOME/bin
sed s/FILENAME/$1/ template.ctl > /tmp/sample.ctl
sqlldr userid=mag/mag control=/tmp/sample.ctl
bash-2.03$ cat template.ctl
LOAD DATA
infile 'FILENAME'
append
INTO TABLE sample
FIELDS TERMINATED BY ","
trailing nullcols
(
filename constant 'FILENAME',
name,
address
)
--
-- this is one such created temporary control file ( for every load)
--
bash-2.03$ cat sample.ctl
cat: cannot open sample.ctl
bash-2.03$ cat /tmp/sample.ctl
LOAD DATA
infile 'file2.dat'
append
INTO TABLE sample
FIELDS TERMINATED BY ","
trailing nullcols
(
filename constant 'file2.dat',
name,
address
)
|
|
|
|