Home » SQL & PL/SQL » SQL & PL/SQL » primary key values with sql loader
primary key values with sql loader [message #146737] Sun, 13 November 2005 10:53 Go to next message
sandhyadeepika
Messages: 1
Registered: November 2005
Location: Chennai
Junior Member
Dear Tom
I have a CSV file from which the data has to be migrated to multiple tables, am able to do it with sql loader but my problem is, I have primary key and foreign key in the table, before inserting the data into the table from the CSV file the primary key values has to be generated (auto generation) in the table.
Is it possible to generate primary key values in the table before inserting the data?if it is possible give me an example.

thanks in advance
sandhya
Re: primary key values with sql loader [message #146741 is a reply to message #146737] Sun, 13 November 2005 13:23 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
You can make use of a database sequence to generate incrementing numbers.
Only caveat is, since a master-detail relationship is involved along with database sequence, you need to use ROWS=1.
With ROWS=1, we asking sql*loader to commit after every row is loaded. If we are talking about a 'big' load, this may potentially slow down your performance.
Re: primary key values with sql loader [message #146743 is a reply to message #146741] Sun, 13 November 2005 16:03 Go to previous messageGo to next message
rleishman
Messages: 3728
Registered: October 2005
Location: Melbourne, Australia
Senior Member
If you use a sequence to allocate the primary key of the master table, then can you get the allocated value into the foreign key of the detail table? I'm not sure that is possible...?

It can be done in a single step (I think), but not with SQL*Loader.

- Create an Externally Organized Table that references your CSV file.
- Write a multi-table INSERT statement that selects from the EOT and the sequence, and INSERTs into both the master and details tables.

_____________
Ross Leishman

Re: primary key values with sql loader [message #146758 is a reply to message #146743] Sun, 13 November 2005 19:54 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
It is possible, with some restrictions.
The OP needs to explain more.
scott@9i > create table parent (c1 number primary key,c2 varchar2(30));

Table created.

scott@9i > create table child (c1 number references parent(c1), c2 varchar2(30));

Table created.

scott@9i > create sequence MySequence start with 1 increment by 1;

Sequence created.

oracle@mutation#cat myctl.ctl
LOAD DATA
INFILE 'mydata.data'
insert
INTO TABLE parent
when (1:1) = 'p'
fields terminated by ',' optionally enclosed by '"' trailing nullcols
(c1 position(1:2) "MySequence.nextval",
 c2 )
into TABLE child
when (1:1) = 'c'
fields terminated by ',' optionally enclosed by '"' trailing nullcols
(c1 position(1:2) "MySequence.currval",
c2 )

oracle@mutation#cat mydata.data
p,parentrecord1
c,childrecord1
p,parentrecord2
c,childrecord2
p,parentrecord3
c,childrecord3
p,parentrecord4
p,parentrecord5
c,childrecord4
c,childrecord5

oracle@mutation#sqlldr userid=scott/tiger control=myctl.ctl rows=1

SQL*Loader: Release 9.2.0.4.0 - Production on Sun Nov 13 20:52:36 2005

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.

Commit point reached - logical record count 1
Commit point reached - logical record count 2
Commit point reached - logical record count 3
Commit point reached - logical record count 4
Commit point reached - logical record count 5
Commit point reached - logical record count 6
Commit point reached - logical record count 7
Commit point reached - logical record count 8
Commit point reached - logical record count 9
Commit point reached - logical record count 10

oracle@mutation#query mutation scott.parent

        C1 C2
---------- ------------------------------
         3 parentrecord3
         4 parentrecord4
         5 parentrecord5
         1 parentrecord1
         2 parentrecord2

oracle@mutation#query mutation scott.child

        C1 C2
---------- ------------------------------
         3 childrecord3
         5 childrecord4
         5 childrecord5
         1 childrecord1
         2 childrecord2

oracle@mutation#
Re: primary key values with sql loader [message #146781 is a reply to message #146758] Sun, 13 November 2005 22:27 Go to previous message
rleishman
Messages: 3728
Registered: October 2005
Location: Melbourne, Australia
Senior Member
Yes! Nice use of the SQL Function. Well done. Much more elegant than my way for learners. The need to commit each row and the inability to use direct-path load just detracts from the elegance though.

_____________
Ross Leishman
Previous Topic: Order varies in Insert from tab1 to tab2 in pl/sql procedure
Next Topic: How to test a storage function?
Goto Forum:
  


Current Time: Sun Jul 07 13:24:53 CDT 2024