pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How do I access the current row from a trigger?
2+ messages / 2 participants
[nested] [flat]

* How do I access the current row from a trigger?
@ 2002-07-03 08:28 Magnus Sjöstrand <mange@NO-SPAMORAMA.mjaunet.org>
  2002-07-03 23:43 ` Re: How do I access the current row from a trigger? Stephan Szabo <sszabo@megazone23.bigpanda.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Magnus Sjöstrand @ 2002-07-03 08:28 UTC (permalink / raw)
  To: pgsql-sql

Hi,

I have a table as:
create table sections (
     id           serial not null primary key,
     parent_id    references sections(id)
);

there is only one row where the parent_id is NULL, and that is the root 
section, all others refers to a section. Now I want to create a trigger, 
so that whenever I delete a section I can delete all sections that has 
the deleted section as parent, which in turn will run the triggers for 
the deleted sections. Thus I will be able to clear an entire branch with 
only one SQL command from the client.

consider the content:
id          parent_id
0           NULL
1           0
2           1
3           2

and when I run the command
delete from sections where id=1;

I want this to trigger a function which will do
delete from sections where parent_id=( 'id' field in the current row[1] )

which in turn will trigger the same function to do
delete from sections where parent_id=( 'id' field in the current row[2] )

is there any way to do this in PostgreSQL? I couldn't find a way of 
refering to the current row in a trigger in the postgresql reference.

Sincerely,
/Magnus Sjöstrand






^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: How do I access the current row from a trigger?
  2002-07-03 08:28 How do I access the current row from a trigger? Magnus Sjöstrand <mange@NO-SPAMORAMA.mjaunet.org>
@ 2002-07-03 23:43 ` Stephan Szabo <sszabo@megazone23.bigpanda.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Stephan Szabo @ 2002-07-03 23:43 UTC (permalink / raw)
  To: Magnus Sjöstrand <mange@NO-SPAMORAMA.mjaunet.org>; +Cc: pgsql-sql


On Wed, 3 Jul 2002, [ISO-8859-1] Magnus Sjöstrand wrote:

> Hi,
>
> I have a table as:
> create table sections (
>      id           serial not null primary key,
>      parent_id    references sections(id)
> );
>
> there is only one row where the parent_id is NULL, and that is the root
> section, all others refers to a section. Now I want to create a trigger,
> so that whenever I delete a section I can delete all sections that has
> the deleted section as parent, which in turn will run the triggers for
> the deleted sections. Thus I will be able to clear an entire branch with
> only one SQL command from the client.

How about just adding ON DELETE CASCADE to the references constraint?

But that doesn't answer the general question about access, so...

> is there any way to do this in PostgreSQL? I couldn't find a way of
> refering to the current row in a trigger in the postgresql reference.

I believe it's somewhat dependant on the procedural language used
to define the trigger function.  In plpgsql, you should be able to use OLD
and NEW to get column values like OLD.id in the case of an after delete
trigger.







^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2002-07-03 23:43 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-07-03 08:28 How do I access the current row from a trigger? Magnus Sjöstrand <mange@NO-SPAMORAMA.mjaunet.org>
2002-07-03 23:43 ` Stephan Szabo <sszabo@megazone23.bigpanda.com>

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