Received: from druid.net (root@druid.net [198.96.119.226]) by hub.org (8.9.3/8.9.3) with ESMTP id SAA35660 for ; Wed, 2 Jun 1999 18:54:50 -0400 (EDT) (envelope-from darcy@druid.net) Received: from localhost (1730 bytes) by druid.net via sendmail with P:stdio/R:bind_hosts/T:inet_zone_bind_smtp (sender: ) (ident using unix) id for ; Wed, 2 Jun 1999 18:54:33 -0400 (EDT) (Smail-3.2.0.104 1998-Nov-20 #4 built 1999-Apr-13) Message-Id: Subject: Re: [SQL] Getting primary key from insert statement In-Reply-To: <551413BBBB03D111BF880060B01AACCE0B38FA@home.priority.net> from "Pham, Thinh" at "Jun 2, 1999 04:46:28 pm" To: tpham@mail.priority.net (Pham Thinh) Date: Wed, 2 Jun 1999 18:54:33 -0400 (EDT) From: "D'Arcy" "J.M." Cain Cc: pgsql-sql@postgreSQL.org Reply-To: pgsql-sql@postgreSQL.org X-Mailer: ELM [version 2.4ME+ PL50 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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 | 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.