Re: Help with trigger

From: Gary Chambers <gwchamb(at)gwcmail(dot)com>
To: Michael Satterwhite <michael(at)weblore(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Help with trigger
Date: 2010-12-27 21:28:42
Message-ID: alpine.OSX.2.01.1012271626130.303@www.clipper.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Michael,

>>> I'm new to PostgreSQL, but have worked with other databases. I'm trying
>>> to write a trigger to default a timestamp column to a fixed interval
>>> before another. The test setup is as follows:

Try this pg_dump of a working example:

CREATE FUNCTION t_listing_startdate() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
if new.d2 is null then
new.d2 := new.d1 - interval '7 day';
end if;
return new;
end;
$$;

CREATE TABLE t (
d1 timestamp without time zone,
d2 timestamp without time zone
);

CREATE TRIGGER t_listing_startdate
BEFORE INSERT OR UPDATE ON t
FOR EACH ROW
EXECUTE PROCEDURE t_listing_startdate();

-- Gary Chambers

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Guillaume Lelarge 2010-12-27 21:34:15 Re: Help with trigger
Previous Message John R Pierce 2010-12-27 21:21:51 Re: Working with v8.3.4 DB using v9.0.1 software