X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 2AA2B5293D for ; Thu, 23 Jun 2005 10:15:45 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 35842-09 for ; Thu, 23 Jun 2005 13:15:40 +0000 (GMT) Received: from hotmail.com (bay103-f39.bay103.hotmail.com [65.54.174.49]) by svr1.postgresql.org (Postfix) with ESMTP id 150815293C for ; Thu, 23 Jun 2005 10:15:37 -0300 (ADT) Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC; Thu, 23 Jun 2005 06:15:41 -0700 Message-ID: Received: from 65.54.174.204 by by103fd.bay103.hotmail.msn.com with HTTP; Thu, 23 Jun 2005 13:15:41 GMT X-Originating-IP: [65.54.174.204] X-Originating-Email: [russellsimpkins@hotmail.com] X-Sender: russellsimpkins@hotmail.com In-Reply-To: <20050622092239.T32802@megazone.bigpanda.com> From: "Russell Simpkins" To: pgsql-sql@postgresql.org Subject: Re: after delete trigger behavior Date: Thu, 23 Jun 2005 09:15:41 -0400 Mime-Version: 1.0 Content-Type: text/plain; format=flowed X-OriginalArrivalTime: 23 Jun 2005 13:15:41.0411 (UTC) FILETIME=[A7B27330:01C577F5] X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.479 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_POST, MSGID_FROM_MTA_HEADER, UPPERCASE_25_50 X-Spam-Level: * X-Archive-Number: 200506/280 X-Sequence-Number: 21881 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;