X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id CA4B811F6C41 for ; Fri, 14 Apr 2006 00:38:22 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=0 required=5 tests=[UPPERCASE_25_50=0] Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id CJ5FACt2Wg77 for ; Fri, 14 Apr 2006 00:38:20 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from nproxy.gmail.com (nproxy.gmail.com [64.233.182.190]) by postgresql.org (Postfix) with ESMTP id 2338611F6C3D for ; Fri, 14 Apr 2006 00:38:19 -0300 (ADT) Received: by nproxy.gmail.com with SMTP id x30so1295147nfb for ; Thu, 13 Apr 2006 20:38:18 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=onIrBR1WUHZR+gLi61uXc29lazO5KfMxxCJekyhJ1VDr0RHJjUT76us4h46Oqcdn+bxQBEdnCVwzupCms5zhGhxuYGzS5G4fvKFsIp6/rfgXAPFCJwpWazVce3ymFX0ZShd2kkx19kKy96b3rmTvJfv3/NTRF/KJ1yyEh+htj3Q= Received: by 10.49.40.4 with SMTP id s4mr392588nfj; Thu, 13 Apr 2006 20:38:18 -0700 (PDT) Received: by 10.49.78.20 with HTTP; Thu, 13 Apr 2006 20:38:18 -0700 (PDT) Message-ID: <226d83de0604132038k66d58e75vc6d290efcba9f2f3@mail.gmail.com> Date: Thu, 13 Apr 2006 23:38:18 -0400 From: "Todd Kennedy" To: pgsql-sql@postgresql.org Subject: Simple plpgsql question MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-Archive-Number: 200604/94 X-Sequence-Number: 24903 Hi, I have, what I hope to be, a simple question about plpgsql. I have a trigger on a table right now that updates a count everytime that a new record is entered into a database (or removed). 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. Example: CREATE TABLE system_info ( id serial PRIMARY KEY, name varchar(255), value varchar(255) ); INSERT INTO system_info (name,value) VALUES ('total_users','0'); CREATE TABLE master ( id serial PRIMARY KEY, name varchar(32) NOT NULL CHECK ( name <> ''), UNIQUE(name) ); CREATE TABLE slave ( id serial PRIMARY KEY, master_id integer REFERENCES master (id), additional_info text ); CREATE OR REPLACE FUNCTION update_users() RETURNS trigger AS $$ BEGIN IF TG_OP =3D 'DELETE' THEN UPDATE system_info SET value=3D(value::integer)-1 WHERE name =3D 'total_users' RETURN OLD; ELSEIF TG_OP =3D 'INSERT' THEN UPDATE system_info SET value=3D(value::integer)+1 WHERE name =3D 'total_users'; =09INSERT INTO slave (master_id) VALUES (THIS IS WHAT I NEED TO KNOW); RETURN NEW; END IF; RETURN NULL; END; $$ LANGUAGE plpgsql; CREATE TRIGGER update_users AFTER INSERT OR DELETE ON master FOR EACH ROW EXECUTE PROCEDURE update_users(); The part I need to know is the INSERT INTO statement in the procedure. Any help would be great. Thanks!