Re: Return key from query - Mailing list pgsql-general

From Szymon Guz
Subject Re: Return key from query
Date
Msg-id AANLkTinW7x5ijN3eTbEyf9qK6oMn9FyoLcL2qdOCEo65@mail.gmail.com
Whole thread Raw
In response to Return key from query  (Jonathan Tripathy <jonnyt@abpni.co.uk>)
List pgsql-general


On 2 November 2010 21:43, Jonathan Tripathy <jonnyt@abpni.co.uk> wrote:
Hi everyone,

When adding a new record, we run an insert query which auto-increments the primary key for the table. However the method (in java) which calls this query must return the newly created key.

Any ideas on how to do this, preferably using a single transaction?

Thanks


Try insert returning, something like this:

INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
   RETURNING did;


regards
Szymon

pgsql-general by date:

Previous
From: Steve Crawford
Date:
Subject: Re: Return key from query
Next
From: Rob Sargent
Date:
Subject: Re: Return key from query