ˈoʊ sɪks
ˈoʊ sɪks

Reputation: 927

Universal SQL construct to retrieve the last row inserted

What would be the correct universal SQL construct to get the last row inserted (or it's primary key). The ID might be autogenerated by a sequence but I do not want to deal with the sequence at all! I need to get the ID by querying the table. Alternatively, INSERT might be somehow extended to return the ID. Assume I am always inserting a single row. The solution should work with most RDBMS!

Upvotes: 0

Views: 464

Answers (1)

Wael Dalloul
Wael Dalloul

Reputation: 23044

the best way is to depend on the sequence like:

select Max(ID) from tableName 

but If you don't want to deal with it, you can add new timestamp column to your table and then select max from that column.

like this way

select Max(TimestampField) from tableName 

Upvotes: 2

Related Questions