Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id BE5FB4763E7 for ; Thu, 19 Sep 2002 16:57:29 -0400 (EDT) Received: from mail.ipatimup.pt (peterpan.ipatimup.pt [194.65.114.74]) by postgresql.org (Postfix) with ESMTP id 1C72B476282 for ; Thu, 19 Sep 2002 16:57:28 -0400 (EDT) Received: from ipatimup.pt (localhost [127.0.0.1]) by mail.ipatimup.pt (8.11.6/IPATIMUP) with SMTP id g8JKNOr28167; Thu, 19 Sep 2002 21:23:25 +0100 X-RAV-AntiVirus: This e-mail has been scanned for viruses on host: mail.ipatimup.pt Received: from 193.136.62.21 (proxying for unknown) (SquirrelMail authenticated user miguel) by www.ipatimup.up.pt with HTTP; Thu, 19 Sep 2002 21:23:25 +0100 (WEST) Message-ID: <6465.193.136.62.21.1032467005.squirrel@www.ipatimup.up.pt> Date: Thu, 19 Sep 2002 21:23:25 +0100 (WEST) Subject: Re: transaction in postgres From: "Miguel Carvalho" To: In-Reply-To: References: X-Priority: 3 Importance: Normal X-MSMail-Priority: Normal Cc: Reply-To: miguel@ipatimup.pt X-Mailer: SquirrelMail (version 1.2.6) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200209/43 X-Sequence-Number: 1498 > Hi averyone, > I'm using a new function to include two query into an unique > transaction. Is this the only way to do it? The following lines show > you my function, the purpose is to return the id_utente field (serial) > of the last insert done: ---------------------------------------- > CREATE FUNCTION "id_from_insert"("varchar", "varchar", "int4", > "bpchar", "varchar", "int4", "int4", "int4", "varchar", "int4", > "bpchar", "varchar", "varchar") RETURNS "int4" AS 'INSERT INTO > g_registrazione_generale ( nome, > cognome, > data_di_nascita, > genere, > indirizzo, > codice_postale, > telefono, > id_documentazione, > email, > data_inserimento, > stato, > velocita_connessione, > istruzione > ) VALUES ( > $1, > $2, > $3, > $4, > $5, > $6, > $7, > $8, > $9, > $10, > $11, > $12, > $13 > ); > SELECT max(g_registrazione_generale.id_utente) FROM > g_registrazione_generale; > ' LANGUAGE 'sql'; Use a query like this: select curval('sequence_name') as nextid; Before you can issue this query, you *must* have started a transaction( begin work ).Its mutch simpler and you dont have to mess with record locking( in your solution there is no garantee that you would get the max value, because another connection may have inserted a new record ). Miguel