views:

534

answers:

3

We have a table 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 we created for the table and uses that for the value of the key on insert. Now we would like to be able to return that value in our insert procedure (PL\SQL), similar to select @@scope_identity in SQL Server. I have been googling all day and basically come up with nothing - anyone been successful with this before?

Thanks

A: 

I think you are looking for a Callable Statement. Here's javadoc if you are trying to get to it from Java.

Alex B
That would work fine if we could populate the output parameter with the newly created key in the PL\SQL - which is basically what I am asking in the question. The key is populated in the insert trigger - now how do I grab that in an insert stored procedure?
Jim Evans
A: 

why not just return the_sequence.currval ?

Unfortunately - the sequence is used by more than one table.
Jim Evans
+3  A: 

I don't know if it works with triggers but the RETURNING clause may be what you're looking for:

INSERT INTO my_table (col_1, col_2)
  VALUES ('foo', 'bar')
  RETURNING pk_id INTO my_variable;
Ken Keenan
Yes - this looks interesting and I will give it a try.
Jim Evans
Worked like a champ with the trigger! I absolutly get the new key value created by the trigger back every time. Thanks.
Jim Evans