Re: [SQL] Getting primary key from insert statement - Mailing list pgsql-sql

From D'Arcy" "J.M." Cain
Subject Re: [SQL] Getting primary key from insert statement
Date
Msg-id m10pJu5-0000bIC@druid.net
Whole thread Raw
In response to Getting primary key from insert statement  ("Pham, Thinh" <tpham@mail.priority.net>)
List pgsql-sql
Thus spake Pham, Thinh
> Hi, is there a way you can get back the primary key (serialized) from an
> insert you just do on a table. For example i have a table called "order" and
> i want to get the pid of that record so i can use it in the "orderdetail"
> table. It would be nice if that same insert statement could also return the
> pid.
> 
> Table structure:
> create table order (pid serial, orddate date);

You realize that order is a keyword, right?

> Insert statement:
> insert into order (date) values ('6/2/1999')

Assuming you change order to something else, the insert returns the OID
of the record just added.  In psql this appears as something like this.

INSERT 36448 1

So, just read back the record.

SELECT pid FROM table WHERE OID = 36448;

The various interfaces give you methods to get this value back.  Read the
docs for whichever interface you are using.

-- 
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.


pgsql-sql by date:

Previous
From: "Mark Wright"
Date:
Subject: Are PL/pgSQL calls atomic?
Next
From: Vadim Mikheev
Date:
Subject: Re: [SQL] Are PL/pgSQL calls atomic?