Home » RDBMS Server » Server Utilities » Loading Multiple Input files into multiple tables (Oracle10g)
Loading Multiple Input files into multiple tables [message #560009] |
Mon, 09 July 2012 03:35 |
|
naveendara
Messages: 11 Registered: June 2012 Location: Hyderabad
|
Junior Member |
|
|
NGFID;RECTYPE;RECNAME
25;7;POLES
PARENT
CHILD;1401;9845075;2020
817;8;SUPPORT
PARENT
CHILD
Required output:-
AREA_SRNO = 1
AREA_NAME = '3rivieres.export.ngf'
File :-mauri.export.ngf
NGFID;RECTYPE;RECNAME
257;7;POLES
PARENT
CHILD;1401;9845075;2020
8174;8;SUPPORT
PARENT
CHILD
Required output:-
AREA_SRNO = 2
AREA_NAME = 'mauri.export.ngf'....etc
CREATE TABLE NGF_REC_LINK
(
AREA_SRNO NUMBER(2),
AREA_NAME VARCHAR2(40),
NGFID NUMBER(20),
TABLENAME VARCHAR2(40),
PARENT VARCHAR2(200),
CHILD VARCHAR2(200)
)
/
CREATE TABLE POLES_7
(
AREA_SRNO NUMBER(2),
AREA_NAME VARCHAR2(50),
NGFID NUMBER(20)
)
/
CREATE TABLE SUPPORT_8
(
AREA_SRNO NUMBER(2),
AREA_NAME VARCHAR2(50),
NGFID NUMBER(20)
)
/
Can you Please find the ctl file (ngf_test.ctl) and modify the ctl file as per my requirment.
Thanks in advance..
[mod-edit: code tags added by bb; please start adding them yourself; see the forum guidelines for how]
-
Attachment: ngf_test.ctl
(Size: 0.96KB, Downloaded 3041 times)
[Updated on: Mon, 09 July 2012 14:36] by Moderator Report message to a moderator
|
|
|
|
|
Re: Loading Multiple Input files into multiple tables [message #560021 is a reply to message #560009] |
Mon, 09 July 2012 15:30 |
|
Barbara Boehmer
Messages: 9104 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
Since you need to use your file names as column values, you will need to make them constants in your control file, so you will need two separate control files. Since the area_srno is also per file, that will need to be a constant as well. The following shows the data files, control files, tables, loads, and results. As stated in your previous thread, you cannot pass the file name to your control file to be used as a column value, so you will need to find a way to create your control file with those values as constants.
-- data file 3rivieres.export.ngf:
NGFID;RECTYPE;RECNAME
25;7;POLES
PARENT
CHILD;1401;9845075;2020
817;8;SUPPORT
PARENT
CHILD
-- data file mauri.export.ngf:
NGFID;RECTYPE;RECNAME
257;7;POLES
PARENT
CHILD;1401;9845075;2020
8174;8;SUPPORT
PARENT
CHILD
-- SQL*Loader control file ngf_test1.ctl:
options(skip=1)
load data
infile '3rivieres.export.ngf'
append
continueif next preserve (1:1) = x'09'
into table ngf_rec_link
FIELDS TERMINATED BY X'9'
trailing nullcols
(area_srno CONSTANT "1",
area_name CONSTANT "3rivieres.export.ngf",
ngfid position (1) terminated by ';',
filler1 boundfiller terminated by ';',
filler2 boundfiller terminated by whitespace,
parent "ltrim (:parent, 'PARENT;')",
child "ltrim (:child, 'CHILD;')",
tablename ":filler2 || '_' || :filler1")
into table poles_7
when (filler1 = '7') and (filler2 = 'POLES')
FIELDS TERMINATED BY X'9'
trailing nullcols
(area_srno CONSTANT "1",
area_name CONSTANT "3rivieres.export.ngf",
ngfid position (1) terminated by ';',
filler1 boundfiller terminated by ';',
filler2 boundfiller ,
filler3 filler ,
filler4 filler )
into table support_8
when (filler1 = '8') and (filler2 = 'SUPPORT')
FIELDS TERMINATED BY X'9'
trailing nullcols
(area_srno CONSTANT "1",
area_name CONSTANT "3rivieres.export.ngf",
ngfid position (1) terminated by ';',
filler1 boundfiller terminated by ';',
filler2 boundfiller ,
filler3 filler ,
filler4 filler)
-- SQL*Loader control file ngf_test2.ctl:
options(skip=1)
load data
infile 'mauri.export.ngf'
append
continueif next preserve (1:1) = x'09'
into table ngf_rec_link
FIELDS TERMINATED BY X'9'
trailing nullcols
(area_srno CONSTANT "2",
area_name CONSTANT "mauri.export.ngf",
ngfid position (1) terminated by ';',
filler1 boundfiller terminated by ';',
filler2 boundfiller terminated by whitespace,
parent "ltrim (:parent, 'PARENT;')",
child "ltrim (:child, 'CHILD;')",
tablename ":filler2 || '_' || :filler1")
into table poles_7
when (filler1 = '7') and (filler2 = 'POLES')
FIELDS TERMINATED BY X'9'
trailing nullcols
(area_srno CONSTANT "2",
area_name CONSTANT "mauri.export.ngf",
ngfid position (1) terminated by ';',
filler1 boundfiller terminated by ';',
filler2 boundfiller ,
filler3 filler ,
filler4 filler )
into table support_8
when (filler1 = '8') and (filler2 = 'SUPPORT')
FIELDS TERMINATED BY X'9'
trailing nullcols
(area_srno CONSTANT "2",
area_name CONSTANT "mauri.export.ngf",
ngfid position (1) terminated by ';',
filler1 boundfiller terminated by ';',
filler2 boundfiller ,
filler3 filler ,
filler4 filler)
options(skip=1)
load data
infile 'mauri.export.ngf'
append
continueif next preserve (1:1) = x'09'
into table ngf_rec_link
FIELDS TERMINATED BY X'9'
trailing nullcols
(area_srno CONSTANT "2",
area_name CONSTANT "mauri.export.ngf",
ngfid position (1) terminated by ';',
filler1 boundfiller terminated by ';',
filler2 boundfiller terminated by whitespace,
parent "ltrim (:parent, 'PARENT;')",
child "ltrim (:child, 'CHILD;')",
tablename ":filler2 || '_' || :filler1")
into table poles_7
when (filler1 = '7') and (filler2 = 'POLES')
FIELDS TERMINATED BY X'9'
trailing nullcols
(area_srno CONSTANT "2",
area_name CONSTANT "mauri.export.ngf",
ngfid position (1) terminated by ';',
filler1 boundfiller terminated by ';',
filler2 boundfiller ,
filler3 filler ,
filler4 filler )
into table support_8
when (filler1 = '8') and (filler2 = 'SUPPORT')
FIELDS TERMINATED BY X'9'
trailing nullcols
(area_srno CONSTANT "2",
area_name CONSTANT "mauri.export.ngf",
ngfid position (1) terminated by ';',
filler1 boundfiller terminated by ';',
filler2 boundfiller ,
filler3 filler ,
filler4 filler)
-- tables:
SCOTT@orcl_11gR2> CREATE TABLE NGF_REC_LINK
2 (
3 AREA_SRNO NUMBER(2),
4 AREA_NAME VARCHAR2(40),
5 NGFID NUMBER(20),
6 TABLENAME VARCHAR2(40),
7 PARENT VARCHAR2(200),
8 CHILD VARCHAR2(200)
9 )
10 /
Table created.
SCOTT@orcl_11gR2>
SCOTT@orcl_11gR2> CREATE TABLE POLES_7
2 (
3 AREA_SRNO NUMBER(2),
4 AREA_NAME VARCHAR2(50),
5 NGFID NUMBER(20)
6 )
7 /
Table created.
SCOTT@orcl_11gR2>
SCOTT@orcl_11gR2> CREATE TABLE SUPPORT_8
2 (
3 AREA_SRNO NUMBER(2),
4 AREA_NAME VARCHAR2(50),
5 NGFID NUMBER(20)
6 )
7 /
Table created.
-- loads:
SCOTT@orcl_11gR2> HOST SQLLDR scott/tiger CONTROL=ngf_test1.ctl LOG=test1.log
SCOTT@orcl_11gR2> HOST SQLLDR scott/tiger CONTROL=ngf_test2.ctl LOG=test2.log
-- results:
SCOTT@orcl_11gR2> COLUMN area_name FORMAT A20
SCOTT@orcl_11gR2> COLUMN tablename FORMAT A9
SCOTT@orcl_11gR2> COLUMN parent FORMAT A6
SCOTT@orcl_11gR2> COLUMN child FORMAT A17
SCOTT@orcl_11gR2> SELECT * FROM ngf_rec_link
2 /
AREA_SRNO AREA_NAME NGFID TABLENAME PARENT CHILD
---------- -------------------- ---------- --------- ------ -----------------
1 3rivieres.export.ngf 25 POLES_7 1401;9845075;2020
1 3rivieres.export.ngf 817 SUPPORT_8
2 mauri.export.ngf 257 POLES_7 1401;9845075;2020
2 mauri.export.ngf 8174 SUPPORT_8
4 rows selected.
SCOTT@orcl_11gR2> SELECT * FROM poles_7
2 /
AREA_SRNO AREA_NAME NGFID
---------- -------------------- ----------
1 3rivieres.export.ngf 25
2 mauri.export.ngf 257
2 rows selected.
SCOTT@orcl_11gR2> SELECT * FROM support_8
2 /
AREA_SRNO AREA_NAME NGFID
---------- -------------------- ----------
1 3rivieres.export.ngf 817
2 mauri.export.ngf 8174
2 rows selected.
|
|
|
Re: Loading Multiple Input files into multiple tables [message #560022 is a reply to message #560021] |
Mon, 09 July 2012 16:24 |
|
Barbara Boehmer
Messages: 9104 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
As to how to generate the control files, if you have your filenames in a table, for example:
SCOTT@orcl_11gR2> select * from files
2 /
FILENAME
------------------------------------------------------------
3rivieres.export.ngf
mauri_export.ngf
2 rows selected.
-- then you can create a sequence and generate the control files, like so:
SCOTT@orcl_11gR2> create sequence ngf_area_srno_seq
2 /
Sequence created.
SCOTT@orcl_11gR2> create or replace directory my_dir as 'c:\my_oracle_files'
2 /
Directory created.
SCOTT@orcl_11gR2> declare
2 v_file utl_file.file_type;
3 v_seq number;
4 begin
5 for r in
6 (select * from files)
7 loop
8 select ngf_area_srno_seq.nextval into v_seq from dual;
9 v_file := utl_file.fopen ('MY_DIR', 'ngf_test' || v_seq || '.ctl', 'W', 32767);
10 utl_file.put_line (v_file, 'options(skip=1)');
11 utl_file.put_line (v_file, 'load data');
12 utl_file.put_line (v_file, 'infile ' || r.filename);
13 utl_file.put_line (v_file, 'append');
14 utl_file.put_line (v_file, 'continueif next preserve (1:1) = x''09''');
15 utl_file.put_line (v_file, 'into table ngf_rec_link');
16 utl_file.put_line (v_file, 'FIELDS TERMINATED BY X''9''');
17 utl_file.put_line (v_file, 'trailing nullcols');
18 utl_file.put_line (v_file, '(area_srno CONSTANT "' || v_seq || '",');
19 utl_file.put_line (v_file, 'area_name CONSTANT "' || r.filename || '",');
20 utl_file.put_line (v_file, 'ngfid position (1) terminated by '';'',');
21 utl_file.put_line (v_file, 'filler1 boundfiller terminated by '';'',');
22 utl_file.put_line (v_file, 'filler2 boundfiller terminated by whitespace,');
23 utl_file.put_line (v_file, 'parent "ltrim (:parent, ''PARENT;'')",');
24 utl_file.put_line (v_file, 'child "ltrim (:child, ''CHILD;'')",');
25 utl_file.put_line (v_file, 'tablename ":filler2 || ''_'' || :filler1")');
26 utl_file.put_line (v_file, 'into table poles_7');
27 utl_file.put_line (v_file, 'when (filler1 = ''7'') and (filler2 = ''POLES'')');
28 utl_file.put_line (v_file, 'FIELDS TERMINATED BY X''9''');
29 utl_file.put_line (v_file, 'trailing nullcols');
30 utl_file.put_line (v_file, '(area_srno CONSTANT "' || v_seq || '",');
31 utl_file.put_line (v_file, 'area_name CONSTANT "' || r.filename || '",');
32 utl_file.put_line (v_file, 'ngfid position (1) terminated by '';'',');
33 utl_file.put_line (v_file, 'filler1 boundfiller terminated by '';'',');
34 utl_file.put_line (v_file, 'filler2 boundfiller ,');
35 utl_file.put_line (v_file, 'filler3 filler ,');
36 utl_file.put_line (v_file, 'filler4 filler )');
37 utl_file.put_line (v_file, 'into table support_8');
38 utl_file.put_line (v_file, 'when (filler1 = ''8'') and (filler2 = ''SUPPORT'')');
39 utl_file.put_line (v_file, 'FIELDS TERMINATED BY X''9''');
40 utl_file.put_line (v_file, 'trailing nullcols');
41 utl_file.put_line (v_file, '(area_srno CONSTANT "' || v_seq || '",');
42 utl_file.put_line (v_file, 'area_name CONSTANT "' || r.filename || '",');
43 utl_file.put_line (v_file, 'ngfid position (1) terminated by '';'',');
44 utl_file.put_line (v_file, 'filler1 boundfiller terminated by '';'',');
45 utl_file.put_line (v_file, 'filler2 boundfiller ,');
46 utl_file.put_line (v_file, 'filler3 filler ,');
47 utl_file.put_line (v_file, 'filler4 filler)');
48 utl_file.fclose (v_file);
49 end loop;
50 end;
51 /
PL/SQL procedure successfully completed.
The above code will generate the two control files in my previous response, using the file names in the table.
|
|
|
|
|
Goto Forum:
Current Time: Fri Feb 07 01:30:09 CST 2025
|