Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return PK value after Insert - Oracle

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,502
US
I have a table in Oracle DB with a Primary Key that gets populated on Insert by a trigger on the table - the trigger gets the next sequence number from a sequence I created for the table and uses that for the value of the PK on insert. Now I would like to be able to return that PK value from my insert statement and use it as Foreign Key in another (child) table. All of that is done in VB code, not a Stored Procedure.

Is there some combination of Insert statement that returns PK used in that Insert? From the same transaction?

I know I can do: [tt]Select Max(PK_Field) as MyPKValue from SomeTable[/tt], but this way I may get somebody else's PK, not necessary mine (if somebody else issued another Insert statement). The same goes if I ask for current value from my sequence, not very safe bet either.


Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Can't you use an output parameter to do this?
 
I have something like this in my code:

Code:
strSQL = "Insert Into SomeTable (FieldA, ...)" & _
    " Values  (123, ...) "

MyConnection.Execute strSQL

What do you mean when you say: "output parameter"?

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top