Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Catching Surrogate Key Value...
"Nick P" <nick_pope_at_nospamhotmail.com> wrote in message news:<uehll0ho3nu616_at_corp.supernews.com>...
> What is the best method for catching the value of a surrogate key (auto
> increment numeric ID) when creating a 'parent' row in one table and 'child'
> rows in another table?
>
> I need to know the value of the surrogate key in the 'parent' row to
> populate the foreign key in the 'child' rows.
>
> E.g.
>
> I have entered some details on screen detailing a new contact organisation
> and a few contacts within that organisation. I hit 'Save' to populate the
> database with this information.
>
> The parent ORGANISATIONS row is created with an internally created numeric
> ID.
>
> I next need to create the CONTACTS rows. How do I get the ORGANISATIONS.ID
> value to put into the foreign key ORG_ID column? What is the best method?
>
> Thank you in advance...
>
> Nick
Nick, Look at the returning clause; it allows you to return any value just inserted via your insert into a bind variable.
Or select the sequence value; move it into your parent insert key field, and then carry it over to your child rows.
UT1> set echo on UT1> set serveroutput on UT1> declare 2 -- 3 v_ctr number := 0 ;
PL/SQL procedure successfully completed.
UT1> select * from marktest where fld1 = 'test' 2 /
FLD1 FLD2 FLD3
---------- ---------- ---------
test 2632 20-MAY-02
HTH -- Mark D Powell -- Received on Mon May 20 2002 - 08:38:31 CDT
![]() |
![]() |