Possible Duplicate:
Get ID of last inserted record in oracle db
I am brand new to oracle, having used SQL Server in the past. I have a stored procedure, and I am trying to do two INSERTs one after the other. The second INSERT requires the ID of the first INSERT. Could someone explain how to do it?
I’m seeing stuff about SEQUENCEs and nextvalue/curval ?
I guess in SQL Server I’d just declare a variable, and use SCOPE_IDENTITY, so I’d be looking to do that.
There are two possibilities. If you are using sequences for number generation, you can get the value from the sequence, like this:
or this
But it’s probably better to use the
returningclause like this:This way, it will work regardless of implementation details. You don’t have to know the name of the sequence, and it will also work with the later introduced identity columns.
The only thing it won’t work for is views with an instead of trigger, but that’s a special case altogether.