Re: Trigger on Insert to Update only newly inserted fields?

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Henry Ortega <juandelacruz(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Trigger on Insert to Update only newly inserted fields?
Date: 2006-08-28 14:57:38
Message-ID: 20060828145737.GA5004@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Mon, Aug 28, 2006 at 10:02:32AM -0400, Henry Ortega wrote:
> I have a On Insert Trigger that updates one of the columns in that same
> table.
>
> Is there a way for the trigger to run only for the newly inserted records?
> Instead of all records in the database?

Row-level INSERT and UPDATE triggers run only for the rows being
inserted or updated. What are you doing that suggests otherwise?

> E.g.:
> ID Start_Date End_Date
> 001 08-01-2006
> 002 08-02-2006
>
> On Insert/Update, Update End_Date=now().
> I want that to run only on new records.or the updated
> record. How can I do this?

Row-level BEFORE triggers can modify the row they're processing --
is that what you're looking for? Something like this?

CREATE FUNCTION trigfunc() RETURNS trigger AS $$
BEGIN
NEW.end_date := current_date;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

If that's not what you mean then please elaborate.

--
Michael Fuhr

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Henry Ortega 2006-08-28 15:53:36 Re: Trigger on Insert to Update only newly inserted fields?
Previous Message Sumeet 2006-08-28 14:29:20 Temporary Views or tables