Re: SQL Help - Obtaining ID of record INSERTED

From: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
To: "josh(at)segrestfarms(dot)com" <josh(at)segrestfarms(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: SQL Help - Obtaining ID of record INSERTED
Date: 2005-08-16 22:08:47
Message-ID: 430263EF.6000302@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

josh(at)segrestfarms(dot)com wrote:
> Greetings.
>
> INSERT INTO
> orderstemp (customerid,datecreated)
> VALUES
> ('5443','8/16/2005 12:00PM')
>
> The table orderstemp has a unique identifier field, orderid (of type
> SERIAL).
>
> How can I obtain the orderid of the record inserted in the INSERT INTO
> statement in postgresql?
>
> MSSQL does it like this:
>
> INSERT INTO
> orderstemp (customerid,datecreated)
> VALUES
> ('5443','8/16/2005 12:00PM')
> SELECT @@identity as orderid

If you are in the same session you can do:

select currval('sequencename_of_orderid');

>
> but this doens't work in postgresql.
>
> Any ideas? Thanks in advance.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly

--
Your PostgreSQL solutions company - Command Prompt, Inc. 1.800.492.2240
PostgreSQL Replication, Consulting, Custom Programming, 24x7 support
Managed Services, Shared and Dedicated Hosting
Co-Authors: plPHP, plPerlNG - http://www.commandprompt.com/

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Tom Lane 2005-08-16 22:24:00 Re: Help with function
Previous Message Jason Minion 2005-08-16 21:42:39 Re: SQL Help - Obtaining ID of record INSERTED