Re: PL/pgSQL loops?

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: "Johnny Jrgensen" <pgsql(at)halfahead(dot)dk>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: PL/pgSQL loops?
Date: 2001-11-29 03:53:30
Message-ID: 20011128194857.G37828-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


Something like the below seemed to make it work for me under
7.2b3:

CREATE FUNCTION cleanup_order(integer,integer)
RETURNS boolean
AS '
-- [INT order_id] cleanup_order ( INT person_id, INT order_id )
-- purpose: when a session is interrupted, the order is left open, when the next session is initiated,
-- transfer the items to a new order, and mark the older order void ( so we can tell if f.x. people abort
-- an order in the payment phase, e.g. indicating they dont like our payment options )

DECLARE
p_id ALIAS FOR $1;
o_id ALIAS FOR $2;
ord record;
itm record;

BEGIN

-- loop through existing open orders from this person, excluding the first, being the active one
FOR ord IN SELECT id FROM ordre WHERE person_id = p_id AND status = 1 AND id != o_id
LOOP
-- loop through items linked to the currently looped order
FOR itm IN SELECT id FROM item WHERE ordre_id = ord.id
LOOP

-- relink item to latest order
UPDATE item SET ordre_id = o_id WHERE id = itm.id;

END LOOP;

-- mark old orders as void
UPDATE ordre SET status = 0 WHERE id = ord.id;

END LOOP;

RETURN true;

END;
'
LANGUAGE 'plpgsql';

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2001-11-29 05:02:01 Re: distinct() vs distinct on ()
Previous Message Haywood J'Bleauxmie 2001-11-29 03:14:43 distinct() vs distinct on ()