pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Russell Simpkins <russellsimpkins@hotmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: after delete trigger behavior
Date: Thu, 23 Jun 2005 09:15:41 -0400
Message-ID: <BAY103-F398954E58D63E29F4BBDD5B5EA0@phx.gbl> (raw)
In-Reply-To: <20050622092239.T32802@megazone.bigpanda.com>

After delete worked and the a foreach execute update seems to work best. 
Below is a satisfactory test set.

-- create test table
CREATE TABLE test1 (
a int,
b int,
c int);
-- create resort function
CREATE OR REPLACE FUNCTION resort_test1() RETURNS TRIGGER AS '
DECLARE
   eachrow RECORD;
   innerrow RECORD;
   sort INT := 0;
BEGIN
    sort := 0;
   FOR eachrow IN EXECUTE ''SELECT * FROM portfolio.test1 WHERE a = '' || 
OLD.a LOOP
       IF eachrow.b != OLD.b THEN
           EXECUTE ''UPDATE portfolio.test1 SET c = '' || sort || '' WHERE a 
= '' || eachrow.a || '' AND b = '' || eachrow.b || '''';
           sort := sort +1;
       END IF;
   END LOOP;
   RETURN OLD;
END;
' language 'plpgsql';

-- create trigger
CREATE TRIGGER u_test1 AFTER DELETE ON portfolio.test1 FOR EACH ROW EXECUTE 
PROCEDURE resort_test1();

-- sample data
insert into test1 values(1,1,0);
insert into test1 values(1,2,1);
insert into test1 values(1,3,2);
insert into test1 values(1,4,3);
insert into test1 values(1,5,4);

insert into test1 values(2,1,0);
insert into test1 values(2,2,1);
insert into test1 values(2,3,2);
insert into test1 values(2,4,3);
insert into test1 values(2,5,4);

-- test delete
delete from test1 where b = 2 or b = 4;
-- view test results
select * from test1 order by a, b, c;





view thread (8+ messages)

Message-ID: <BAY103-F398954E58D63E29F4BBDD5B5EA0@phx.gbl>
Permalink:  ../BAY103-F398954E58D63E29F4BBDD5B5EA0@phx.gbl/
Also on:    postgresql.org/message-id/BAY103-F398954E58D63E29F4BBDD5B5EA0@phx.gbl

 · 

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: russellsimpkins@hotmail.com
  Subject: Re: after delete trigger behavior
  In-Reply-To: <BAY103-F398954E58D63E29F4BBDD5B5EA0@phx.gbl>

* 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