X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 20C98DAFF1 for ; Tue, 22 Nov 2005 11:57:07 -0400 (AST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 66291-07 for ; Tue, 22 Nov 2005 15:57:10 +0000 (GMT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from svr2.postgresql.org (svr2.postgresql.org [65.19.161.25]) by svr1.postgresql.org (Postfix) with ESMTP id 049F6DAFFC for ; Tue, 22 Nov 2005 11:57:03 -0400 (AST) Received: from mail45.e.nsc.no (mail45.e.nsc.no [193.213.115.45]) by svr2.postgresql.org (Postfix) with ESMTP id B0CA4F0B20 for ; Tue, 22 Nov 2005 15:57:08 +0000 (GMT) Received: from [192.168.0.57] (062016183238.customer.alfanett.no [62.16.183.238]) (authenticated bits=0) by mail45.nsc.no (8.13.5/8.13.5) with ESMTP id jAMFv4PY026186 for ; Tue, 22 Nov 2005 16:57:05 +0100 (CET) From: "Leif B. Kristensen" To: pgsql-sql@postgresql.org Subject: Re: Triggers Date: Tue, 22 Nov 2005 16:44:35 +0100 User-Agent: KMail/1.8.1 References: In-Reply-To: X-Face: %iea8o"[SI'kMDS.`hz7lU=?\^lC9J-]CVjo; f{4-C:$?uVUEjyN~uYFn%*aH_c3d09U>=?utf-8?q?x=0A=09TXtaoj=7EMV=2E8PgG7hik9=3BcCT2N6+u?=@Y{wH\p3*_f~FH9k,Wi74e[_l%.7b^'zJ)8S'^G<(=?utf-8?q?=3D=0A=09zw8=5CPMC3=27=7BKl9=2EzL*ks!b6wA?=,c~'F`k&Vq|ELDGa|4 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200511/200 X-Sequence-Number: 23451 On Tuesday 22 November 2005 17:25, Achilleus Mantzios wrote: >O Leif B. Kristensen =DD=E3=F1=E1=F8=E5 =F3=F4=E9=F2 Nov 22, 2005 : >> I'm trying to understand triggers. I have read the documentation in >> the manual as well as the few pages in the Douglas book about the >> subject, but I don't see how to implement a trigger that simply >> updates a 'last_edit' date field in my 'persons' table whenever I do >> an insert or update into my 'participants' table; that is a trigger >> that basically does an "UPDATE persons SET 'last_edit' =3D NOW() WHERE >> persons.person_id =3D participants.person_fk". Is that even possible? > >smth like: >foodb=3D# CREATE or REPLACE FUNCTION upd_dad() RETURNS "trigger" AS ' >foodb'# BEGIN >foodb'# UPDATE dad set lastedit=3Dnow() where id=3Dnew.dadid; >foodb'# RETURN new; >foodb'# END; >foodb'# ' >foodb-# LANGUAGE plpgsql; >CREATE FUNCTION >foodb=3D# CREATE TRIGGER upd_dad_tg AFTER INSERT OR UPDATE ON kid FOR > EACH ROW EXECUTE PROCEDURE upd_dad(); leif=3D> create or replace function update_last_edit() returns trigger as=20 $$ leif$> begin leif$> update persons set last_edit=3Dnow() where person_id=3Dnew.person_fk; leif$> return new; leif$> end; leif$> $$ language plpgsql; CREATE FUNCTION leif=3D> create trigger update_last_edit after insert or update on=20 participants leif-> for each row execute procedure update_last_edit(); CREATE TRIGGER leif=3D> insert into participants (participant_id,person_fk) values (1,1); ERROR: record "new" has no field "last_edit" CONTEXT: PL/pgSQL function "process_last_edited" line 2 at assignment leif=3D>=20 What am I missing? =2D-=20 Leif Biberg Kristensen | Registered Linux User #338009 http://solumslekt.org/ | Cruising with Gentoo/KDE