From yudha@BonBon.net Wed Feb 19 13:20:19 2003 X-Original-To: pgsql-admin@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id ACC1C475458 for ; Wed, 19 Feb 2003 08:20:19 -0500 (EST) Received: from babyruth.hotpop.com (babyruth.hotpop.com [204.57.55.14]) by postgresql.org (Postfix) with ESMTP id 0CD77475425 for ; Wed, 19 Feb 2003 08:20:19 -0500 (EST) Received: from bonbon.net (kubrick.hotpop.com [204.57.55.16]) by babyruth.hotpop.com (Postfix) with SMTP id 9E7C021235B for ; Wed, 19 Feb 2003 13:20:08 +0000 (UTC) Received: from yudha (unknown [61.5.113.36]) by smtp-2.hotpop.com (Postfix) with SMTP id A60971B889D for ; Wed, 19 Feb 2003 13:20:00 +0000 (UTC) Message-ID: <01c801c2d81a$414f4aa0$ea00a8c0@yudha> From: "Yudha Setiawan" To: Subject: How to avoid the Mutating Triggers ?? Date: Wed, 19 Feb 2003 19:59:22 +0700 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_01C0_01C2D851.6567D790" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.00.2919.6700 X-MIMEOLE: Produced By Microsoft MimeOLE V5.00.2919.6700 X-HotPOP: ----------------------------------------------- Sent By HotPOP.com FREE Email Get your FREE POP email at www.HotPOP.com ----------------------------------------------- X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/307 X-Sequence-Number: 7505 This is a multi-part message in MIME format. ------=_NextPart_000_01C0_01C2D851.6567D790 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Dear Master, I've some problem with this situation. It's hard to explain what kind of problem is, but i try to tell you=20 with my limitation on english. How do I "recalculate" one of my field, if i updated one of the record that's have position in the middle of all record. Here my Ilustration is; ---------------------------------------- Poin-|FdDate | Code| IN |OUT| Qty | ter. |---------------------------------- 1 |01/01/2003 | 001 | 5 | 0 | 5 |=20 2 |01/02/2003 | 001 | 3 | 0 | 8 | 3 |01/03/2003 | 001 | 0 | 1 | 7 | 4 |01/06/2003 | 001 | 100 | 0 | 117 | 5 |00107/2003 | 001 | 0 | 1 | 118 | 6 |00108/2003 | 001 | 0 | 1 | 119 | 7 |00109/2003 | 001 | 0 | 1 | 120 | ---------------------------------------- And what i want is, update the record=20 on Pointer no 4. on "IN" is changed from 100 to 10 And Qty is Automaticly change from 117 to 17. like this; ---------------------------------------- Poin-|FdDate | Code| IN |OUT| Qty | ter. |---------------------------------- 4 |01/06/2003 | 001 | 10 | 0 | 17 | ---------------------------------------- Following with record below it, and become=20 like this; ---------------------------------------- Poin-|FdDate | Code| IN |OUT| Qty | ter. |---------------------------------- 5 |00107/2003 | 001 | 0 | 1 | 18 | 6 |00108/2003 | 001 | 0 | 1 | 19 | 7 |00109/2003 | 001 | 0 | 1 | 20 | ---------------------------------------- I've been trying using cursor inside of my trigger, It didn't work and it's=20 "mutating". i thought my trigger is=20 working recursive. It's very Urgent=20 for me. I've been migrating Database=20 from Ms-SQL, so far so great, till=20 this Problem I've been spending my=20 3 Days with No-Result.=20 It's time to Give-up.=20 So gimme your hand please...??? Thank's to you and,=20 GOD Bless You All. Yudha Setiawan. VoIP=20 No. : 628899911223344 ------=_NextPart_000_01C0_01C2D851.6567D790 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Dear Master,
I've some problem with this=20 situation.
It's hard to explain what kind=20 of
problem is, but i try to tell you= =20
with my limitation on=20 english.
 
How do I "recalculate" one of
my field, if i updated one of
the record that's have=20 position
in the middle of all record.
Here my Ilustration is;
----------------------------------------
Poin-|FdDate   &nbs= p; |=20 Code| IN  |OUT| Qty |
ter.=20 |----------------------------------
1    |01/01/2003 | = 001 |=20=20 5  | 0 |   5 |
2    |01/02/2003 | = 001 |=20=20 3  | 0 |   8 |
3    |01/03/2003 | = 001 |=20=20 0  | 1 |   7 |
4    |01/06/2003 | = 001 | 100=20 | 0 | 117 |
5    |00107/2003 | = 001 |=20=20 0  | 1 | 118 |
6    |00108/2003 | = 001 |=20=20 0  | 1 | 119 |
7    |00109/2003 | = 001 |=20=20 0  | 1 | 120 |
----------------------------------------
And what i want is, update the record
on Pointer no 4. on "IN" is changed from
100 to 10 And Qty is Automaticly change
from 117 to 17. like this;
----------------------------------------
Poin-|FdDate   &nbs= p; |=20 Code| IN  |OUT| Qty |
ter.=20 |----------------------------------
4    |01/06/2003 | = 001 | 10=20=20 | 0 |  17 |
----------------------------------------
Following with record below it, and become
like this;
----------------------------------------
Poin-|FdDate   &nbs= p; |=20 Code| IN  |OUT| Qty |
ter.=20 |----------------------------------
5    |00107/2003 | = 001 |=20 0  | 1 |  18 |
6    |00108/2003 | = 001 |=20 0  | 1 |  19 |
7    |00109/2003 | = 001 |=20 0  | 1 |  20 |
----------------------------------------
 
I've been trying using cursor insi= de=20 of
my trigger, It didn't work and it'= s=20
"mutating". i thought my trigger i= s=20
working recursive. It's very Urgen= t=20
for me. I've been migrating Databa= se=20
from Ms-SQL, so far so great, till
this Problem I've been spending=20 my
3 Days with No-Result.
It's time to Give-up.
So gimme your hand please...???
 
Thank's to you and,
GOD Bless You All.
 
Yudha Setiawan.
VoIP=20
    No.        :=20 628899911223344
------=_NextPart_000_01C0_01C2D851.6567D790-- From sszabo@megazone23.bigpanda.com Wed Feb 19 16:29:18 2003 X-Original-To: pgsql-admin@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 70D8447580B for ; Wed, 19 Feb 2003 11:29:18 -0500 (EST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id E39D1475425 for ; Wed, 19 Feb 2003 11:29:17 -0500 (EST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 7FA7ED622; Wed, 19 Feb 2003 08:29:22 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 752AF5C03; Wed, 19 Feb 2003 08:29:22 -0800 (PST) Date: Wed, 19 Feb 2003 08:29:22 -0800 (PST) From: Stephan Szabo To: Yudha Setiawan Cc: Subject: Re: How to avoid the Mutating Triggers ?? In-Reply-To: <01c801c2d81a$414f4aa0$ea00a8c0@yudha> Message-ID: <20030219082219.G34399-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/309 X-Sequence-Number: 7507 On Wed, 19 Feb 2003, Yudha Setiawan wrote: > How do I "recalculate" one of > my field, if i updated one of > the record that's have position > in the middle of all record. > Here my Ilustration is; > ---------------------------------------- > Poin-|FdDate | Code| IN |OUT| Qty | > ter. |---------------------------------- > 1 |01/01/2003 | 001 | 5 | 0 | 5 | > 2 |01/02/2003 | 001 | 3 | 0 | 8 | > 3 |01/03/2003 | 001 | 0 | 1 | 7 | > 4 |01/06/2003 | 001 | 100 | 0 | 117 | > 5 |00107/2003 | 001 | 0 | 1 | 118 | > 6 |00108/2003 | 001 | 0 | 1 | 119 | > 7 |00109/2003 | 001 | 0 | 1 | 120 | > ---------------------------------------- > And what i want is, update the record > on Pointer no 4. on "IN" is changed from > 100 to 10 And Qty is Automaticly change > from 117 to 17. like this; > ---------------------------------------- > Poin-|FdDate | Code| IN |OUT| Qty | > ter. |---------------------------------- > 4 |01/06/2003 | 001 | 10 | 0 | 17 | > ---------------------------------------- > Following with record below it, and become > like this; > ---------------------------------------- > Poin-|FdDate | Code| IN |OUT| Qty | > ter. |---------------------------------- > 5 |00107/2003 | 001 | 0 | 1 | 18 | > 6 |00108/2003 | 001 | 0 | 1 | 19 | > 7 |00109/2003 | 001 | 0 | 1 | 20 | > ---------------------------------------- > > I've been trying using cursor inside of > my trigger, It didn't work and it's > "mutating". i thought my trigger is > working recursive. It's very Urgent > for me. I've been migrating Database > from Ms-SQL, so far so great, till > this Problem I've been spending my > 3 Days with No-Result. > It's time to Give-up. > So gimme your hand please...??? Well, what have you tried? For the simple case (pointer is not changing), a not terribly efficient version might be something like: create or replace function dorecalcupd() returns trigger as 'declare last int; begin SELECT INTO last qty FROM foo where pointer < NEW."pointer" ORDER BY pointer desc LIMIT 1; NEW.qty := last + NEW."in" - NEW."out"; if (NEW.pointer = OLD.POINTER AND (NEW."in" != OLD."in" or NEW."out" != OLD."out")) then UPDATE foo set qty = (select sum(f."in") - sum(f."out") from foo f where f.pointerNEW.pointer; end if; return NEW; end;' language 'plpgsql'; In this case the trigger does cause requests to the same trigger for the later rows, but since only qty is changing it does nothing. It's more complicated when you deal with the pointer moving case. From yudha@BonBon.net Thu Feb 20 02:24:42 2003 X-Original-To: pgsql-admin@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 3DD35474E5C for ; Wed, 19 Feb 2003 21:24:42 -0500 (EST) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with SMTP id 24267-05 for ; Wed, 19 Feb 2003 21:24:31 -0500 (EST) Received: from snickers.hotpop.com (snickers.hotpop.com [204.57.55.49]) by postgresql.org (Postfix) with ESMTP id 8CDAC474E4F for ; Wed, 19 Feb 2003 21:24:31 -0500 (EST) Received: from bonbon.net (kubrick.hotpop.com [204.57.55.16]) by snickers.hotpop.com (Postfix) with SMTP id 61275722D1 for ; Thu, 20 Feb 2003 02:24:33 +0000 (UTC) Received: from yudha (unknown [61.5.113.68]) by smtp-1.hotpop.com (Postfix) with SMTP id 1C6D82F8113; Thu, 20 Feb 2003 02:24:27 +0000 (UTC) Message-ID: <004c01c2d887$d801b3d0$ea00a8c0@yudha> From: "Yudha Setiawan" To: , "Stephan Szabo" References: <20030219082219.G34399-100000@megazone23.bigpanda.com> Subject: Re: How to avoid the Mutating Triggers ?? Date: Thu, 20 Feb 2003 09:29:00 +0700 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.00.2919.6700 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6700 X-HotPOP: ----------------------------------------------- Sent By HotPOP.com FREE Email Get your FREE POP email at www.HotPOP.com ----------------------------------------------- X-Virus-Scanned: by amavisd-new X-Archive-Number: 200302/325 X-Sequence-Number: 7523 Dear Szabo, You were wrote; > Well, what have you tried? For the simple > case (pointer is not changing), a not terribly > efficient version might be something like: > > create or replace function dorecalcupd() returns trigger as > 'declare > last int; > begin > SELECT INTO last qty FROM foo where pointer < NEW."pointer" ORDER BY > pointer desc LIMIT 1; > NEW.qty := last + NEW."in" - NEW."out"; > if (NEW.pointer = OLD.POINTER AND (NEW."in" != OLD."in" or NEW."out" != > OLD."out")) then > UPDATE foo set qty = (select sum(f."in") - sum(f."out") from foo f where > f.pointer where pointer>NEW.pointer; > end if; > return NEW; > end;' language 'plpgsql'; > > In this case the trigger does cause requests to the same trigger for the > later rows, but since only qty is changing it does nothing. > > It's more complicated when you deal with the pointer moving case. Thank's a Lot for all milist especialy for dear Stephan Szabo. I'm Sorry "Pointer" on my ilustration "IS NOT a FIELD". it's just information for me, to show you where is exactly the position of record / Pointer that I want to Change. Perhaps you have another best one, please...?? Thank You very much and, GOD Bless You All. Yudha Setiawan. VoIP No. : 628899911223344 From sszabo@megazone23.bigpanda.com Thu Feb 20 02:37:43 2003 X-Original-To: pgsql-admin@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id CBE01474E61 for ; Wed, 19 Feb 2003 21:37:43 -0500 (EST) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with SMTP id 28116-04 for ; Wed, 19 Feb 2003 21:37:33 -0500 (EST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id F087E474E4F for ; Wed, 19 Feb 2003 21:37:32 -0500 (EST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id CE552D628; Wed, 19 Feb 2003 18:37:36 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id C40EE5C03; Wed, 19 Feb 2003 18:37:36 -0800 (PST) Date: Wed, 19 Feb 2003 18:37:36 -0800 (PST) From: Stephan Szabo To: Yudha Setiawan Cc: Subject: Re: How to avoid the Mutating Triggers ?? In-Reply-To: <004c01c2d887$d801b3d0$ea00a8c0@yudha> Message-ID: <20030219183512.P40199-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new X-Archive-Number: 200302/326 X-Sequence-Number: 7524 On Thu, 20 Feb 2003, Yudha Setiawan wrote: > Dear Szabo, You were wrote; > > > Well, what have you tried? For the simple > > case (pointer is not changing), a not terribly > > efficient version might be something like: > > > > create or replace function dorecalcupd() returns trigger as > > 'declare > > last int; > > begin > > SELECT INTO last qty FROM foo where pointer < NEW."pointer" ORDER BY > > pointer desc LIMIT 1; > > NEW.qty := last + NEW."in" - NEW."out"; > > if (NEW.pointer = OLD.POINTER AND (NEW."in" != OLD."in" or NEW."out" != > > OLD."out")) then > > UPDATE foo set qty = (select sum(f."in") - sum(f."out") from foo f where > > f.pointer > where pointer>NEW.pointer; > > end if; > > return NEW; > > end;' language 'plpgsql'; > > > > In this case the trigger does cause requests to the same trigger for the > > later rows, but since only qty is changing it does nothing. > > > > It's more complicated when you deal with the pointer moving case. > > Thank's a Lot for all milist especialy for dear Stephan Szabo. I'm Sorry > "Pointer" on my ilustration "IS NOT a FIELD". it's just information for > me, to show you where is exactly the position of record / Pointer that > I want to Change. > Perhaps you have another best one, please...?? Well, what's the key that you're ordering by? Use that instead of pointer in the above. If you have no ordering key or the key isn't guaranteed to be unique, the original question doesn't have an answer because there is no implict order to rows in a table.