Saturday, 6 February 2010

How To Get Last Inserted Row ID In PostgreSQL

Imagine we have a test table.

CREATE TABLE test (id SERIAL, name TEXT);

Use the RETURNING statement in the INSERT statement.

INSERT INTO test (name) VALUES ('My Name') RETURNING id;

 id 
----
  1 

INSERT INTO test (name) VALUES ('My Name 1') RETURNING id;

 id 
----
  2 

More info in Postgres manual.

No comments:

Post a Comment