On Update (trigger hint)

Lists: pgsql-general
From: MaRCeLO PeReiRA <gandalf_mp(at)yahoo(dot)com(dot)br>
To: pgsql <pgsql-general(at)postgresql(dot)org>
Subject: On Update (trigger hint)
Date: 2004-02-27 16:50:22
Message-ID: 20040227165022.81353.qmail@web20210.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi guys,

Please, give me some advices on how to do the
following:

I have the following table:

CREATE TABLE products (
id SERIAL,
description TEXT,
lastupdate date
);

Well, I would like to update the column "lastupdate"
with the value "now()" on every UPDATE executed on a
row of this table.

Do I have to create a function to do it? Can you help
me?

Regards,

Marcelo

______________________________________________________________________

Yahoo! Mail - O melhor e-mail do Brasil! Abra sua conta agora:
http://br.yahoo.com/info/mail.html


From: Michael Vester <michaelvester(at)shaw(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: On Update (trigger hint)
Date: 2004-02-27 18:34:40
Message-ID: 45M%b.629122$X%5.171962@pd7tw2no
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

MaRCeLO PeReiRA wrote:

> Hi guys,
>
> Please, give me some advices on how to do the
> following:
>
> I have the following table:
>
> CREATE TABLE products (
> id SERIAL,
> description TEXT,
> lastupdate date
> );
>
> Well, I would like to update the column "lastupdate"
> with the value "now()" on every UPDATE executed on a
> row of this table.
>
> Do I have to create a function to do it? Can you help
> me?
>
> Regards,
>
> Marcelo
<snip>

/* Your function */
CREATE FUNCTION set_lastchg() RETURNS opaque AS '
BEGIN
NEW.lastupdate = now();
RETURN NEW;
END;
'LANGUAGE 'plpgsql';

/* and the triggers that will use it, All my main tables have a
lastupdate column and a trigger to execute set_lastchg()
whenever the row is changed */

CREATE TRIGGER lastclubchg_trig
BEFORE INSERT OR UPDATE ON club FOR EACH
ROW EXECUTE PROCEDURE set_lastchg();

CREATE TRIGGER lastownerchg_trig
BEFORE INSERT OR UPDATE ON owner FOR EACH
ROW EXECUTE PROCEDURE set_lastchg();

--
11:25am up 4 days, 1:32, 1 user, load average: 1.29, 1.38, 1.29
To email me, change .com to .ca Linux Counter Registration #126647