Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ID of last inserted record in oracle db

Tags:

oracle

I want to retrieve the id of a newly inserted record with an auto incrementing id column (using the sequence and trigger method). What is the standard way to do this?

like image 510
haymansfield Avatar asked Jun 28 '10 08:06

haymansfield


People also ask

How do I get the last inserted ID in SQL?

SELECT @@IDENTITY @@IDENTITY will return the last identity value entered into a table in your current session. While @@IDENTITY is limited to the current session, it is not limited to the current scope.

How do you find the last inserted record in a table in Oracle?

You could use ORA_ROWSCN to get the last scn for each block on the table as an *approximate* means of seeing when things happened, or the flashback query syntax to get transaction information, but you are limited to the undo_retention settings on your database.

How do I find the last inserted record?

If you are AUTO_INCREMENT with column, then you can use last_insert_id() method. This method gets the ID of the last inserted record in MySQL. Insert some records in the table using insert command.


1 Answers

Use the PL/SQL RETURNING clause:

insert into mytable (...) values (...) returning id into v_id; 
like image 92
Tony Andrews Avatar answered Sep 17 '22 12:09

Tony Andrews