pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: David M <davidgm0@ucia.gov>
To: pgsql-sql@postgresql.org
Subject: Re: trigger to maintain relationships
Date: Wed, 11 Dec 2002 17:02:40 -0800
Message-ID: <web-2277708@davinci.ethosmedia.com> (raw)
In-Reply-To: <3DF7777A.41DDD72B@ucia.gov>

David,

> FYI, join should've looked like:
> 
> create function pr_tr_i_nodes() returns opaque
> as '
>     insert into ancestors
>     select NEW.node_id, ancestor_id
>     from NEW left outer join ancestors on (NEW.parent_id =
> ancestors.node_id);
> 
>     return NEW;'
> language 'plpgsql';
> create trigger tr_i_nodes after insert
>     on nodes for each row
>     execute procedure pr_tr_i_nodes();

Ummm ... no.

Within the trigger produre, NEW is a record variable, and its fields
are values.  You cannot SELECT from NEW.  You're also missing the parts
of a PLPGSQL procedure.  What you want is:

create function pr_tr_i_nodes() returns opaque
> as '
DECLARE v_ancestor INT;
BEGIN
SELECT ancestor_id INTO v_ancestor
FROM ancestors WHERE ancestors.node_id = NEW.parent_id;
INSERT INTO ancestors
VALUES ( NEW.node_id, v_ancestor );
>     return NEW;
END;'
> language 'plpgsql';

-Josh Berkus



view thread (3+ messages)

Message-ID: <web-2277708@davinci.ethosmedia.com>
Permalink:  ../web-2277708@davinci.ethosmedia.com/
Also on:    postgresql.org/message-id/web-2277708@davinci.ethosmedia.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: josh@agliodbs.com, davidgm0@ucia.gov
  Subject: Re: trigger to maintain relationships
  In-Reply-To: <web-2277708@davinci.ethosmedia.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox