0

Can someone please help me. I need to load data from a .dat file into a Table.

.DAT file has Col1, Col2, Col3

Table have Col1, Col4

What I want is

 Table.Col4 = DAT File Col2*Col3

How can I achieve this via SQLLDR and CTL file. Note Columns in DAT File can be of Variable length.

eg DAT File: 


  110000002 , 1                                        , 7500                                       
  110000003 , 1                                        , 7500                                       
  110000008 , 1                                        , 7500                                       
  110000028 , 1                                        , 7500                                       
  110000054 , 1                                        , 7500                                       
  110000055 , 1                                        , 7500                                       
  110000082 , 1                                        , 7500                                       
  110000095 , 1                                        , 7500                        
Saty
  • 22,443
  • 7
  • 33
  • 51
ShobhitSaxena
  • 73
  • 5
  • 12

3 Answers3

0

I guess you could try this ctl file :

load data
infile 'my_file.dat'
truncate
into table my_table
fields terminated by ','
(
  COL1,
  COL2 boundfiller,
  COL3 boundfiller,
  COL4 expression ":COL2 * :COL3"
)
Dimitri Mockelyn
  • 1,535
  • 2
  • 11
  • 17
Pwin
  • 3
  • 2
0

Hi Here is code you need to put in control file. it will work as col1 will get value from first string till ',' then second and third column value get saved into variable INPUTCOL2 and INPUTCOL3 and at the end we are combining these two values in col4 which is column of you table with multiplication.

LOAD DATA
INFILE 'mydata.dat'
INSERT
INTO TABLE MY_table
fields terminated by ','
trailing nullcols
(
 COL1,
 INPUTCOL2 BOUNDFILLER,
 INPUTCOL3 BOUNDFILLER,
 COL4 ":INPUTCOL2*:INPUTCOL3"
)
vishnu sable
  • 328
  • 1
  • 7
0

Use of functions in a SQL*Loader control file

When using PLSQL functions in SQL*Loader, use quotations.

Here is column number four from the control file:

COL4 ":COL2*:COL3"

To see this used let us create a test table, MY_TABLE.

  1. DDL for our test table, MY_TABLE.

    CREATE TABLE SCOTT.MY_TABLE ( "COL1" NUMBER(9,0) NOT NULL ENABLE, "COL2" NUMBER(6,0), "COL3" NUMBER(6,0), "COL4" NUMBER(8,0) );

  2. create a test control file with this approach. I included your sample data:

LOAD DATA INFILE * BADFILE 'MY_TABLE.bad' DISCARDFILE 'MY_TABLE.dsc' INSERT INTO TABLE MY_TABLE FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY ' ' TRAILING NULLCOLS ( COL1, COL2, COL3, COL4 ":COL2*:COL3" ) BEGINDATA 110000002 , 1 , 7500 110000003 , 2 , 7500 110000008 , 1 , 7500 110000028 , 3 , 7500 110000054 , 1 , 7500 110000055 , 1 , 7500 110000082 , 4 , 7500 110000095 , 5 , 7500

  1. Invoke sqlldr command referencing the control file.

  2. spooled results:

    SCOTT@tst>SELECT 2 * 3 FROM 4 my_table;

     COL1       COL2       COL3       COL4
---------- ---------- ---------- ----------
 110000002          1       7500       7500
 110000003          2       7500      15000
 110000008          1       7500       7500
 110000028          3       7500      22500
 110000054          1       7500       7500
 110000055          1       7500       7500
 110000082          4       7500      30000
 110000095          5       7500      37500

8 rows selected.
Patrick Bacon
  • 4,490
  • 1
  • 26
  • 33