|
|
Question : cascading inserts; passing new seq # to child tables
|
|
|
|
for the following tables, tableA (tableA_pk, tableB_fk, col1) tableB (tableB_pk,col1,col2,col3,col4,col5) tableC (tableA_fk, col1, col2, col3) tableD (tableD_pk, col1,col2,col3,col4,col5,col6, col7)
what logic can you envision to do the following?
we can create 2 sequence for tableB and tableA; but how do you pass on the new sequence number to the child records?
--create new records in tableB based on tableD match INSERT into tableB b select createnewpk, d.col3,d.col4,d.col5,d.col6, d.col7 from tableB b , tableD d where d.col1 = b.col1 and d.col2 and b.col2
--create new record for child records in tableA (no conditions involved) INSERT into tableA a select createnewpk, b.generated_newpk, col1
--create new record for child records in tableC but update the data based on tableD match. INSERT into tableC c select a.generated_newpk,.d.col5, d.col6, d.col7 from tableC c, tableD d where c.col1 = d.col5 and c.col2 = d.col6 and c.col3= d.col7
|
|
|
|
Answer : cascading inserts; passing new seq # to child tables
|
|
|
|
When you use a sequence you use the following construct to get a new value from it:
seqname.nextval
You can then refer to the current value of the sequence (without generating new values) by using the following:
seqname.currval
So by including the currval in you insert statements you associate the child rows with their parents.
|
|
|
|
|