Re: Basic Trigger

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Roberto de Amorim <roberto(at)smartbit(dot)inf(dot)br>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Basic Trigger
Date: 2002-11-18 11:40:03
Message-ID: 20021118033907.P48668-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, 18 Nov 2002, Roberto de Amorim wrote:

> Hi all,
>
> I did some Trigger to set default values for each row interted.
> so, I'm using NEW.ID = NEXTVAL(''products_id_seq'');
> but the column do not assum this value...
> must I execute any other command before to set a value for NEW?
>
> CREATE OR REPLACE FUNCTION "f_products"() RETURNS "opaque" AS '
> BEGIN
> NEW.ID = NEXTVAL(''products_id_seq'');
> RETURN NEW;
> END' LANGUAGE 'plpgsql'
>
> CREATE TRIGGER "t_classes" AFTER INSERT ON "products" FOR EACH ROW EXECUTE
> PROCEDURE f_products();

Apart from the fact that serial or a default is easier, if you want to
modify the row to be inserted (or the updated value of an update), you
want a BEFORE trigger, not an AFTER one.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message snpe 2002-11-18 13:08:12 Re: [GENERAL] DECLARE CURSOR
Previous Message Einar Karttunen 2002-11-18 11:30:15 Re: Basic Trigger