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 1E84011F6B2F for ; Fri, 14 Apr 2006 06:04:07 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: 1.352 X-Spam-Level: * X-Spam-Status: No, score=1.352 required=5 tests=[BAYES_00=-2.599, FORGED_RCVD_HELO=0.135, SPF_HELO_SOFTFAIL=2.432, SPF_SOFTFAIL=1.384, 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 fYXFalbTuviR for ; Fri, 14 Apr 2006 06:03:51 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from rhino.esc1.com (dpc67142225134.direcpc.com [67.142.225.134]) by postgresql.org (Postfix) with ESMTP id B179011F6AB0 for ; Fri, 14 Apr 2006 06:03:44 -0300 (ADT) Received: from rhino.esc1.com (localhost.localdomain [127.0.0.1]) by rhino.esc1.com (8.12.11/8.12.11) with ESMTP id k3E93D2a019304 for ; Fri, 14 Apr 2006 05:03:14 -0400 Received: from localhost (localhost [[UNIX: localhost]]) by rhino.esc1.com (8.12.11/8.12.11/Submit) id k3E93C0L019302 for pgsql-sql@postgresql.org; Fri, 14 Apr 2006 05:03:12 -0400 From: Terry Lee Tucker Organization: Enhanced Solutions Computing To: pgsql-sql@postgresql.org Subject: Re: Simple plpgsql question Date: Fri, 14 Apr 2006 05:03:10 -0400 User-Agent: KMail/1.5.3 References: <226d83de0604132038k66d58e75vc6d290efcba9f2f3@mail.gmail.com> In-Reply-To: <226d83de0604132038k66d58e75vc6d290efcba9f2f3@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200604140503.10335.terry@esc1.com> X-Archive-Number: 200604/97 X-Sequence-Number: 24906 On Thursday 13 April 2006 11:38 pm, "Todd Kennedy" thus communicated: --> 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 = 'DELETE' THEN --> UPDATE system_info SET value=(value::integer)-1 WHERE name = --> 'total_users' --> RETURN OLD; --> ELSEIF TG_OP = 'INSERT' THEN --> UPDATE system_info SET value=(value::integer)+1 WHERE name = --> 'total_users'; --> INSERT 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! --> --> ---------------------------(end of broadcast)--------------------------- --> TIP 4: Have you searched our list archives? --> --> http://archives.postgresql.org --> INSERT INTO slave (master_id) VALUES (new.id); The buffer NEW contains all the "new" data.