Received: from aristote2.infomaniak.ch (aristote2.infomaniak.ch [212.23.249.227]) by postgresql.org (Postfix) with ESMTP id 88656475FC2 for ; Fri, 2 Aug 2002 08:59:51 -0400 (EDT) Received: from ced001 (unverified [213.221.146.181]) by aristote2.infomaniak.ch (Rockliffe SMTPRA 4.5.6) with ESMTP id ; Fri, 2 Aug 2002 14:59:44 +0200 From: =?iso-8859-1?Q?C=E9dric_Dufour_=28Cogito_Ergo_Soft=29?= To: "Ken Corey" , "Peter Atkins" Cc: Subject: Re: Returning PK of first insert for second insert use. Date: Fri, 2 Aug 2002 14:59:44 +0200 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2911.0) Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 In-Reply-To: <1027969192.2354.206.camel@kenlinux.bithub.org> X-Archive-Number: 200208/31 X-Sequence-Number: 8877 You can retrieve the last inserted sequence value using: currval('t_task_task_id_seq') This is connection safe, so you get the the last ID inserted by YOUR connection. > -----Original Message----- > From: pgsql-sql-owner@postgresql.org > [mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Ken Corey > Sent: Monday, July 29, 2002 20:59 > To: Peter Atkins > Cc: 'pgsql-sql@postgresql.org' > Subject: Re: [SQL] Returning PK of first insert for second insert use. > > > On Mon, 2002-07-29 at 19:32, Peter Atkins wrote: > > I have two tables t_proj, t_task see below: > > > > CREATE TABLE t_proj ( > > proj_id SERIAL NOT NULL, > > PRIMARY KEY (proj_id), > > task_id integer(12), > > user_id integer(6), > > title varchar(35), > > description varchar(80) > > ); > > > > CREATE TABLE t_task ( > > task_id SERIAL NOT NULL, > > PRIMARY KEY (task_id), > > title varchar(35), > > description varchar(80) > > ); > > > > When I insert into t_task I need to return the task_id (PK) for > that insert > > to be used for the insert into the t_proj table. > > > > I tried using RESULT_OID but I have no idea how to obtain the > true PK using > > this opague id. Below is the procedure I tried to use. > > Since the primary key of the first table is a SERIAL, it's really > defined as something like this: > > create table t_task ( > task_id int4 not null default nextval('t_task_task_id_seq'), > ... > > Which means that you can predict what the next value will be, store that > in a temporary var, and then insert it into both tables... > > CREATE OR REPLACE FUNCTION insertTask (varchar, varchar) > RETURNS INTEGER AS ' > > DECLARE > -- local variables > oid1 INTEGER; > retval INTEGER; > tempvar int4; > > BEGIN > select into tempvar nextval(''t_task_task_id_seq''); > > INSERT INTO t_task (task_id, title, description) > VALUES (tempvar,$1, $2); > > -- Everything has passed, return id as pk > RETURN tempvar; > END; > ' LANGUAGE 'plpgsql'; > > WARNING: this is not guaranteed to be the correct syntax, I didn't > create the tables and the function to test it, but I do this kind of > thing all the time in my functions. > > -- > Ken Corey CTO http://www.atomic-interactive.com 07720 440 731 > > > ---------------------------(end of broadcast)--------------------------- > TIP 6: Have you searched our list archives? > > http://archives.postgresql.org >