Thread: returning id

returning id

From
Marc Fromm
Date:

Can I use “RETURNING id” in an insert statement with postgresql version 8.1.10?

I cannot find a clear example on how to use it to capture the id created by the insert statement into a variable in PHP.

 

Many examples are like this but don’t state how to access the returned id:

INSERT INTO Addresses (address, city, state, postalcode) VALUES ('123 Main St.','Springfield','MA','01109') RETURNING id;

 

Thanks

 

Marc

Re: returning id

From
Lonni J Friedman
Date:
On Tue, Sep 20, 2011 at 12:31 PM, Marc Fromm <Marc.Fromm@wwu.edu> wrote:
> Can I use “RETURNING id” in an insert statement with postgresql version
> 8.1.10?
>
> I cannot find a clear example on how to use it to capture the id created by
> the insert statement into a variable in PHP.
>
>
>
> Many examples are like this but don’t state how to access the returned id:
>
> INSERT INTO Addresses (address, city, state, postalcode) VALUES ('123 Main
> St.','Springfield','MA','01109') RETURNING id;
>

Is there a reason why you can't use the currval function:
select currval('id_seq')

Re: returning id

From
Tom Lane
Date:
Marc Fromm <Marc.Fromm@wwu.edu> writes:
> Can I use "RETURNING id" in an insert statement with postgresql version 8.1.10?

No.  I don't recall just when RETURNING was added, but for sure it was
after 8.1.

            regards, tom lane

Re: returning id

From
Guillaume Lelarge
Date:
On Tue, 2011-09-20 at 15:55 -0400, Tom Lane wrote:
> Marc Fromm <Marc.Fromm@wwu.edu> writes:
> > Can I use "RETURNING id" in an insert statement with postgresql version 8.1.10?
>
> No.  I don't recall just when RETURNING was added, but for sure it was
> after 8.1.
>

I checked. It appeared in 8.2. Sooner than I expected actually.


--
Guillaume
  http://blog.guillaume.lelarge.info
  http://www.dalibo.com