Something like the below seemed to make it work for me under
7.2b3:
CREATE FUNCTION cleanup_order(integer,integer)RETURNS booleanAS ' -- [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
idFROM ordre WHERE person_id = p_id AND status = 1 AND id != o_id LOOP -- loop through items linked to the
currentlylooped 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';