X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id AF3535536C for ; Mon, 21 Mar 2005 12:29:01 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 70366-01 for ; Mon, 21 Mar 2005 12:28:49 +0000 (GMT) Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by svr1.postgresql.org (Postfix) with SMTP id 21C0C545F4 for ; Mon, 21 Mar 2005 12:28:47 +0000 (GMT) Received: (qmail 24187 invoked from network); 21 Mar 2005 12:27:42 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.36?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 21 Mar 2005 12:27:42 -0000 Message-ID: <423EBDFA.9040507@fastcrypt.com> Date: Mon, 21 Mar 2005 07:28:42 -0500 From: Dave Cramer Reply-To: pg@fastcrypt.com Organization: Postgres International User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Nico Cc: pgsql-jdbc@postgresql.org Subject: Re: getting a sequence value References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.036 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/170 X-Sequence-Number: 12562 you can either retrieve the nextval before inserting and insert it or let the default constraint insert it for you and retrieve it using currval 1) select nextval('public."tblTable1_FieldID_seq"'::text) then insert this value into table 1 and you will know the value beforehand 2) allow the insert to go through select currval('public."tblTable1_FieldID_seq"'::text) and then use this to insert into table 2 FYI currval does the "right" thing. It is guaranteed to be the value your connection retrieved from the sequence. Dave Nico wrote: >Hi, >I have two tables where the second table has a foreign key to the first one. >When I insert a row in the first one, I need the value of the ID generated >by a sequence in the first table to insert it in the foreign key column in >the second table. Except I don't know how to do this in Java/PostgreSQL... >Anyone knows how? >Table data: >Table 1: >Field1 (integer) default: nextval('public."tblTable1_FieldID_seq"'::text) >other field... >Table 2: >Field1 (integer) default: nextval('public."tblTable2_FieldID_seq"':text) >Field2 (integer) foreign key that references to Field1 in Table 1... > >So when I do this: >INSERT INTO "tblTable1"(other fields...) VALUES(...); >how do I know what to put in here? >INSERT INTO "tblTable2"("Field2", other fields...) VALUES(???, ...); > >I know in MS SQL server and Oracle one uses an old and new resultset to >resolve this issue, but haven't a clue how to do this in Java/PostgreSQL... > >Nico. > > > >---------------------------(end of broadcast)--------------------------- >TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org > > > > -- Dave Cramer http://www.postgresintl.com 519 939 0336 ICQ#14675561