X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx2.hub.org [200.46.204.254]) by postgresql.org (Postfix) with ESMTP id 1F84111F6B31 for ; Fri, 14 Apr 2006 06:16:44 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: 1.218 X-Spam-Level: * X-Spam-Status: No, score=1.218 required=5 tests=[BAYES_00=-2.599, SPF_HELO_SOFTFAIL=2.432, SPF_SOFTFAIL=1.384, UNPARSEABLE_RELAY=0.001, UPPERCASE_25_50=0] Received: from postgresql.org ([200.46.204.71]) by localhost (mx2.hub.org [200.46.204.254]) (amavisd-new, port 10024) with ESMTP id rxySPFi9UsYW for ; Fri, 14 Apr 2006 06:16:27 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from fep03.ttnet.net.tr (mail.ttnet.net.tr [212.175.13.129]) by postgresql.org (Postfix) with ESMTP id 8217111F6CF7 for ; Fri, 14 Apr 2006 06:16:23 -0300 (ADT) Received: from (192.168.8.103) by AVGW-I-1.ttnet.net.tr via smtp id 529c_20cf0f5a_cb6d_11da_9831_001422175f0e; Fri, 14 Apr 2006 07:14:13 +0300 Received: from alamut ([85.102.40.91]) by fep03.ttnet.net.tr with ESMTP id <20060414091130.FFWS1269.fep03.ttnet.net.tr@alamut>; Fri, 14 Apr 2006 12:11:30 +0300 Date: Fri, 14 Apr 2006 12:13:44 +0300 From: Volkan YAZICI To: Todd Kennedy Cc: pgsql-sql@postgresql.org Subject: Re: Simple plpgsql question Message-ID: <20060414091344.GA209@alamut> References: <226d83de0604132038k66d58e75vc6d290efcba9f2f3@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <226d83de0604132038k66d58e75vc6d290efcba9f2f3@mail.gmail.com> User-Agent: Mutt/1.4.2.1i X-NAI-Spam-Rules: 1 Rules triggered BAYES_10=-0.7 X-Archive-Number: 200604/98 X-Sequence-Number: 24907 On Apr 13 11:38, Todd Kennedy wrote: > What I'd also like to do is have it create a new row in a different > table using the automatically assigned id as a reference, but I'm > unsure of how to obtain the id of the newly created row in the first > table. If I understand you right, you're refering to a SERIAL column with id. If so, you can use currval() function over related SEQUENCE. Because of INSERT/DELETE and trigger will be executed in the same session, they'll be able to see current value of related sequence. Below is an example about this: BEGIN; CREATE SEQUENCE trig_t_seq START 1; CREATE TABLE trig_t ( id bigint NOT NULL DEFAULT nextval('trig_t_seq'), inf int ); CREATE FUNCTION trig_t_row_count() RETURNS trigger AS $$ BEGIN IF TG_OP = 'INSERT' THEN RAISE NOTICE 'Current SEQUENCE value: %', currval('trig_t_seq'); END IF; RETURN NEW; END $$ LANGUAGE plpgsql; CREATE TRIGGER trig_t_row_count_trig AFTER INSERT ON trig_t FOR EACH ROW EXECUTE PROCEDURE trig_t_row_count(); INSERT INTO trig_t (inf) VALUES (10); INSERT INTO trig_t (inf) VALUES (20); INSERT INTO trig_t (inf) VALUES (30); ROLLBACK; Regards.