Re: [SQL] Getting primary key from insert statement

From: "D'Arcy" "J(dot)M(dot)" Cain <darcy(at)druid(dot)net>
To: tpham(at)mail(dot)priority(dot)net (Pham Thinh)
Cc: pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] Getting primary key from insert statement
Date: 1999-06-02 22:54:33
Message-ID: m10pJu5-0000bIC@druid.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at){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.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Vadim Mikheev 1999-06-03 01:24:56 Re: [SQL] Are PL/pgSQL calls atomic?
Previous Message Mark Wright 1999-06-02 22:18:19 Are PL/pgSQL calls atomic?