pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stephan Szabo <sszabo@megazone23.bigpanda.com>
To: "Johnny Jørgensen" <pgsql@halfahead.dk>
Cc: pgsql-sql@postgresql.org
Subject: Re: PL/pgSQL loops?
Date: Wed, 28 Nov 2001 19:53:30 -0800 (PST)
Message-ID: <20011128194857.G37828-100000@megazone23.bigpanda.com> (raw)
In-Reply-To: <200111290305140952.021C953A@mail.halfahead.dk>


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';





view thread (13+ messages)  latest in thread

Message-ID: <20011128194857.G37828-100000@megazone23.bigpanda.com>
Permalink:  ../20011128194857.G37828-100000@megazone23.bigpanda.com/
Also on:    postgresql.org/message-id/20011128194857.G37828-100000@megazone23.bigpanda.com

 · 

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: sszabo@megazone23.bigpanda.com, pgsql@halfahead.dk
  Subject: Re: PL/pgSQL loops?
  In-Reply-To: <20011128194857.G37828-100000@megazone23.bigpanda.com>

* 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