Re: Trigger

From: Franco Bruno Borghesi <franco(at)akyasociados(dot)com(dot)ar>
To: "Stefan Sturm" <mailling(at)anrath(dot)info>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Trigger
Date: 2003-04-10 16:12:49
Message-ID: 200304101312.50325.franco@akyasociados.com.ar
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I usually use plpgsql to program my triggers. The plpgsql manual is here:
http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=plpgsql.html

Section 19.9 specifically talks about triggers.

Anyway, here is an example of what you said you needed:

--my table
CREATE TABLE mytest (id SERIAL, name TEXT, lastchange TIMESTAMP);

--function to set the value of the field 'lastChange' to current system tyme
CREATE OR REPLACE FUNCTION mytest_set_lastChange() RETURNS TRIGGER AS '
BEGIN
NEW.lastChange:=current_timestamp;
RETURN NEW;
END; ' LANGUAGE 'plpgsql';

--trigger that calls mytest_set_lastChange after inserts or updates
CREATE TRIGGER mytest_tg1 BEFORE INSERT OR UPDATE ON mytest FOR EACH ROW
EXECUTE PROCEDURE mytest_set_lastChange();

INSERT INTO mytest (name) VALUES ('peter');
SELECT * FROM mytest;
id | name | lastchange
----+-------+----------------------------
1 | peter | 2003-04-10 13:10:16.993779
(1 row)

UPDATE mytest SET name='joe';
id | name | lastchange
----+------+----------------------------
1 | joe | 2003-04-10 13:11:10.787253
(1 row)

hope it helps.

On Wednesday 10 April 2024 11:58, you wrote:
> t want to update to fields on instert and update with the system
> time...

In response to

  • Trigger at 2003-04-10 14:58:37 from Stefan Sturm

Browse pgsql-sql by date

  From Date Subject
Next Message joostje 2003-04-10 16:29:02 Re: estimates for nested loop very wrong?
Previous Message Jan Wieck 2003-04-10 16:09:38 Re: INSERT INTO ... SELECT (PostgreSQL vs. MySQL)