How retrieve ids inserted into a table with a sequence?

A forum for discussing DataStage<sup>®</sup> basics. If you're not sure where your question goes, start here.

Moderators: chulett, rschirm, roy

Post Reply
ju
Participant
Posts: 5
Joined: Mon May 28, 2007 10:04 am

How retrieve ids inserted into a table with a sequence?

Post by ju »

Hi,
I must insert few rows into an oracle table. But in this table, there is a trigger which generate the id with a sequence:
" IF :new.c_thrd_in_no IS NULL THEN
SELECT THRD_c_thrd_in_no.nextval
INTO :new.c_thrd_in_no
FROM dual;
END IF;"

And i would like to retrieve the new ids which correspond to the rows inserted and put these ids into a dynamic relashionship file.


I succeed to retrieve into a file the new id to insert, but i don't know how to merge this file with my other file which contain the others data to insert. (the 2 files don't have the same number of columns).

if anybody have an idea...

thanks
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

There must be a key column in your file that contains the generated keys and the other file. Do a lookup on the keys. Or thats not the case? If not, then how do you want to attach the keys generated by one table with the other file?
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Have an extra column in the target table that identifies the run (or the run date) when the row was inserted. Use this to identify the rows inserted by a particular run.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
ju
Participant
Posts: 5
Joined: Mon May 28, 2007 10:04 am

Post by ju »

Use the date is not possible because the date is generated too. And if i selected the rows inserted in the table, i loose the id correspond in mys old file.

1)
Exemple:
I have a file:
"IDFILE","NAME",...
"TRY1","HENRY",...
"TRY2","ZIDANE",...

and i must insert these rows in a table where ids and date of creation are generated.

In the table, data inserted wille be for exemple:
IDTABLE,NAME,...,CREATIONDATE
22650,"HENRY",...,30/05/07 10:05:15
22651,"ZIDANE",...,30/05/07 10:05:15

And i would like to have a new file with the old and the new Ids.
"IDFILE","IDTABLE"
"TRY1","22650"
"TRY2","22651"


2)
Or another solution would be to insert directly in my first file the idTABLE that will be generated thanks this request:
SELECT THRD_c_thrd_in_no.nextval FROM dual; -->22650
The file will look like to
"IDFILE","NAME",...,"IDTABLE"
"TRY1","HENRY",...,22650
"TRY2","ZIDANE",...,22651

and inserted these data into the table with the IDTABLE. (Its possible, because in the trigger, we test if the IDTABLE is empty or not)

But how know how much select i must to do, and how merge the result of the request with the first file?



So, the first solution is perhaps less difficult...
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Go with #2. Don't use the trigger. Pull the sequence value in your job so you know what it is before you use it. Then you can write the new ID to your file as well.
-craig

"You can never have too many knives" -- Logan Nine Fingers
Post Reply