Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id D73E89F9F0A for ; Thu, 1 Feb 2007 14:06:16 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 62892-07 for ; Thu, 1 Feb 2007 14:06:13 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 0D38A9FB1DC for ; Thu, 1 Feb 2007 14:06:13 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from smtp.compugraf.com.br (cgexc04.compugraf.com.br [200.194.112.27]) by svr4.postgresql.org (Postfix) with ESMTP id 5D34D5AF8E5 for ; Thu, 1 Feb 2007 18:06:12 +0000 (GMT) X-MimeOLE: Produced By Microsoft Exchange V6.5 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Subject: Insert Data and autonumeric field Date: Thu, 1 Feb 2007 16:09:22 -0300 Message-ID: X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [SQL] Performance Problem with sub-select using array Thread-Index: AcbHsBAPRxgE0jdgQFi7+Swd7HqarwACt7sQH55d/NA= From: =?iso-8859-1?Q?Andr=E9_Jos=E9_Guergolet?= To: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=BAYES_50 X-Spam-Level: X-Archive-Number: 200702/2 X-Sequence-Number: 27336 Hi everybody, I need to insert a row in a table and get the Id of this row ( My = primary key ). Example: INSERT INTO table1 (date, field2, field3) VALUES = (now,'value2','value3'); SELECT last_value FROM seq_table1; I'm running each command apart. My application retrieves the last_value = and uses it in another command: INSERT INTO table2 (pk1, field1, field2, field3) VALUES ( = last_value_variable, 'value1','value2','value3'); PROBLEM: Many clients are getting duplicated IDs. What is the best way of doing this? I tried a function: CREATE OR REPLACE FUNCTION fu_insertrow(int4, text) RETURNS int4 AS $BODY$ DECLARE i_lastvalue INTEGER; BEGIN INSERT INTO table1 (date, field1, field2) values (now(),$1,'$2'); SELECT i_lastvalue INTO i_lastvalue from "seq_ChamadaId"; RETURN i_lastvalue; END; $BODY$ LANGUAGE 'plpgsql' VOLATILE; Using this function: SELECT fu_insertrow(value2, value3); The app gets the return value of the function above and uses it in my = insert: INSERT INTO table2 (pk1, field1, field2, field3) VALUES ( = function_return_variable, 'value1','value2','value3'); Suggestions? Thanks, Andr=E9 Guergolet