Home » RDBMS Server » Server Utilities » Master child sequence load in sql loader
Master child sequence load in sql loader [message #664794] Fri, 04 August 2017 14:57 Go to next message
karth
Messages: 2
Registered: August 2017
Junior Member
I have a 2 file data.dat and control.ctl files need to load in 2 different tables.

data.dat file need to load in table A

control.ctl file need to load in table b

source id is a common column in both the tables its a primary and foreign key relationship.

I need to load a sequence number in the source id column to have master child relationship.


Please advise

Re: Master child sequence load in sql loader [message #664801 is a reply to message #664794] Fri, 04 August 2017 23:29 Go to previous messageGo to next message
Michel Cadot
Messages: 65208
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Example?

Re: Master child sequence load in sql loader [message #664877 is a reply to message #664801] Wed, 09 August 2017 15:25 Go to previous messageGo to next message
karth
Messages: 2
Registered: August 2017
Junior Member
I have 2 flat files in 2 different income directory (data and control files) which will come every month as

<ddmmyy>.dat file one data is

Amount date
500 1-03-17
300 1-03-17

<ddmmyy>.ctl file two data is

count sum
2 800

Need to insert <ddmmyy>.dat file in table T1 which contain columns as

Source file_name Amount date
1 <ddmmyy>.dat 500 1-03-17
1 <ddmmyy>.dat 300 1-03-17


Need to insert <ddmmyy>.ctl file in table T2 which contain columns as


Source file_name count sum
1 <ddmmyy>.ctl 2 800


Then I need to reconcile the count and sum in T2 with T1 table based on join with source.

For next month I will archive the file in the directory and the new files will be placed

<ddmmyy>.dat file one data is

Amount date
100 1-04-17
200 1-04-17

<ddmmyy>.ctl file two data is

count sum
2 300

Need to insert <ddmmyy>.dat file in table T1 which contain columns as

Source file_name Amount date
2 <ddmmyy>.dat 100 1-04-17
2 <ddmmyy>.dat 200 1-04-17


Need to insert <ddmmyy>.ctl file in table T2 which contain columns as


Source file_name count sum
2 <ddmmyy>.ctl 2 300




Please advise
Re: Master child sequence load in sql loader [message #664880 is a reply to message #664877] Thu, 10 August 2017 01:02 Go to previous messageGo to next message
Michel Cadot
Messages: 65208
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Align the columns in result.
Also always post your Oracle version, with 4 decimals, as solution depends on it.

With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.
Re: Master child sequence load in sql loader [message #665022 is a reply to message #664877] Thu, 17 August 2017 18:42 Go to previous message
Barbara Boehmer
Messages: 8751
Registered: November 2002
Location: California, USA
Senior Member
You could create a sequence, then use that sequence for loading of t2.source, then select the max(source) from t2 when loading t1. The following example assumes that your dates in your file names are the sysdate, therefore they don't match the actual names used.

-- data files you provided:
SCOTT@orcl_12.1.0.2.0> host type 010317.dat
Amount date
500 1-03-17
300 1-03-17

SCOTT@orcl_12.1.0.2.0> host type 010317.ctl
count sum
2 800

SCOTT@orcl_12.1.0.2.0> host type 010417.dat
Amount date
100 1-04-17
200 1-04-17

SCOTT@orcl_12.1.0.2.0> host type 010417.ctl
count sum
2 300

-- SQL*Loader control files:
SCOTT@orcl_12.1.0.2.0> host type t1.ctl
options(skip=1)
load data
append into table t1
fields terminated by whitespace trailing nullcols
( amount
, date_col   date 'dd-mm-yy'
, file_name  "to_char (sysdate, 'ddmmyy') || '.dat'"
, source     expression "(select max(source) from t2)" )

SCOTT@orcl_12.1.0.2.0> host type t2.ctl
options(skip=1)
load data
append into table t2
fields terminated by whitespace trailing nullcols
( count
, sum
, file_name  "to_char (sysdate, 'ddmmyy') || '.ctl'"
, source     "test_seq.nextval" )

-- tables:
SCOTT@orcl_12.1.0.2.0> create table t1
  2    (source     number,
  3     file_name  varchar2(10),
  4     amount     number,
  5     date_col   date)
  6  /

Table created.

SCOTT@orcl_12.1.0.2.0> create table t2
  2    (source     number,
  3     file_name  varchar2(10),
  4     count      number,
  5     sum        number)
  6  /

Table created.

-- sequence:
SCOTT@orcl_12.1.0.2.0> create sequence test_seq
  2  /

Sequence created.

-- first load, results, and reconciliation:
SCOTT@orcl_12.1.0.2.0> host sqlldr scott/tiger control=t2.ctl log=test2.log data=010317.ctl

SQL*Loader: Release 12.1.0.2.0 - Production on Thu Aug 17 16:44:52 2017

Copyright (c) 1982, 2014, Oracle and/or its affiliates.  All rights reserved.

Path used:      Conventional
Commit point reached - logical record count 1

Table T2:
  1 Row successfully loaded.

Check the log file:
  test2.log
for more information about the load.

SCOTT@orcl_12.1.0.2.0> select * from t2
  2  /

    SOURCE FILE_NAME       COUNT        SUM
---------- ---------- ---------- ----------
         1 170817.ctl          2        800

1 row selected.

SCOTT@orcl_12.1.0.2.0> host sqlldr scott/tiger control=t1.ctl log=test1.log data=010317.dat

SQL*Loader: Release 12.1.0.2.0 - Production on Thu Aug 17 16:44:53 2017

Copyright (c) 1982, 2014, Oracle and/or its affiliates.  All rights reserved.

Path used:      Conventional
Commit point reached - logical record count 2

Table T1:
  2 Rows successfully loaded.

Check the log file:
  test1.log
for more information about the load.

SCOTT@orcl_12.1.0.2.0> select * from t1
  2  /

    SOURCE FILE_NAME      AMOUNT DATE_COL
---------- ---------- ---------- ---------------
         1 170817.dat        500 Wed 01-Mar-2017
         1 170817.dat        300 Wed 01-Mar-2017

2 rows selected.

SCOTT@orcl_12.1.0.2.0> select t2.source, count(*) t1count, t2.count t2count, sum(t1.amount) t1sum, t2.sum t2sum
  2  from   t1, t2
  3  where  t1.source = t2.source
  4  group  by t2.source, t2.count, t2.sum
  5  /

    SOURCE    T1COUNT    T2COUNT      T1SUM      T2SUM
---------- ---------- ---------- ---------- ----------
         1          2          2        800        800

1 row selected.

-- second load, results, and reconciliation:
SCOTT@orcl_12.1.0.2.0> host sqlldr scott/tiger control=t2.ctl log=test4.log data=010417.ctl

SQL*Loader: Release 12.1.0.2.0 - Production on Thu Aug 17 16:44:55 2017

Copyright (c) 1982, 2014, Oracle and/or its affiliates.  All rights reserved.

Path used:      Conventional
Commit point reached - logical record count 1

Table T2:
  1 Row successfully loaded.

Check the log file:
  test4.log
for more information about the load.

SCOTT@orcl_12.1.0.2.0> select * from t2
  2  /

    SOURCE FILE_NAME       COUNT        SUM
---------- ---------- ---------- ----------
         1 170817.ctl          2        800
         2 170817.ctl          2        300

2 rows selected.

SCOTT@orcl_12.1.0.2.0> host sqlldr scott/tiger control=t1.ctl log=test3.log data=010417.dat

SQL*Loader: Release 12.1.0.2.0 - Production on Thu Aug 17 16:44:56 2017

Copyright (c) 1982, 2014, Oracle and/or its affiliates.  All rights reserved.

Path used:      Conventional
Commit point reached - logical record count 2

Table T1:
  2 Rows successfully loaded.

Check the log file:
  test3.log
for more information about the load.

SCOTT@orcl_12.1.0.2.0> select * from t1
  2  /

    SOURCE FILE_NAME      AMOUNT DATE_COL
---------- ---------- ---------- ---------------
         1 170817.dat        500 Wed 01-Mar-2017
         1 170817.dat        300 Wed 01-Mar-2017
         2 170817.dat        100 Sat 01-Apr-2017
         2 170817.dat        200 Sat 01-Apr-2017

4 rows selected.

SCOTT@orcl_12.1.0.2.0> select t2.source, count(*) t1count, t2.count t2count, sum(t1.amount) t1sum, t2.sum t2sum
  2  from   t1, t2
  3  where  t1.source = t2.source
  4  group  by t2.source, t2.count, t2.sum
  5  /

    SOURCE    T1COUNT    T2COUNT      T1SUM      T2SUM
---------- ---------- ---------- ---------- ----------
         1          2          2        800        800
         2          2          2        300        300

2 rows selected.

[Updated on: Thu, 17 August 2017 18:51]

Report message to a moderator

Previous Topic: Data Pump export - ORA-39126 and ORA-12899
Next Topic: expdp & impdp
Goto Forum:
  


Current Time: Fri Nov 24 18:03:44 CST 2017

Total time taken to generate the page: 0.03209 seconds