Skip site navigation (1) Skip section navigation (2)

function/trigger problems

From: Rory Campbell-Lange <rory(at)campbell-lange(dot)net>
To: Postgresql Novice List <pgsql-novice(at)postgresql(dot)org>
Subject: function/trigger problems
Date: 2002-06-18 09:02:09
Message-ID: 20020618090209.GA5600@campbell-lange.net (view raw or flat)
Thread:
Lists: pgsql-novice
I am trying to update a timestamp value in a related table using
functions and triggers, but failing miserably! I'd be grateful for some
help.

I'm trying to update the tstamp value in a whenever an  insert is done
on b where b.a_id = a.id. So 
	INSERT INTO b (a_id) values (1)
should update the tstamp value in a using the trigger.
My function is failing at the 'WHERE' clause. 

-----------------------------------------------------------------------

CREATE TABLE "a" (
	"id" SERIAL,
	"tstamp" timestamp
);

CREATE TABLE "b" (
	"id" SERIAL,
	"a_id" integer,
	"tstamp" timestamp default current_timestamp.
);


CREATE FUNCTION "fnTU" () RETURNS opaque AS 'BEGIN
UPDATE a SET tstamp = new.tstamp;
WHERE new.a_id = id;
RETURN new;
END' LANGUAGE 'plpgsql';

CREATE TRIGGER "trgTU" AFTER INSERT ON "b"  FOR EACH ROW EXECUTE PROCEDURE "fnTU" ();

COPY "a" FROM stdin;
1	2002-06-18 09:47:44.734527+01
2	2002-06-18 09:47:44.734527+01

COPY "b" FROM stdin;
1	1	2002-06-18 09:47:44.734527+01

-- 
Rory Campbell-Lange 
<rory(at)campbell-lange(dot)net>
<www.campbell-lange.net>

pgsql-novice by date

Next:From: Rory Campbell-LangeDate: 2002-06-18 09:14:26
Subject: function delete problems
Previous:From: Tony GriffithsDate: 2002-06-18 08:24:14
Subject: Client-side libraries for fmgr invocation

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group