CREATE or REPLACE FUNCTION xfer (amount integer, debit TEXT, credit TEXT) RETURNS integer AS $$ -- updated 2005 for gwb table DECLARE user TEXT; -- local variables, balance INTEGER; BEGIN -- Transfer the amount from debit acct to credit account, if both exist! select into user current_user::char(5); -- who called the function update gwb set vault = vault - amount where acct=debit::char(5); -- want an exception if either user is not valid! --------- IF not FOUND THEN RAISE EXCEPTION 'Debit customer % cannot be found!', debit; END IF; update gwb set vault = vault + amount where acct=credit::char(5); IF not FOUND THEN RAISE EXCEPTION 'Credit customer % cannot be found!', credit; END IF; select vault into balance from gwb where acct=user::char(5); IF not FOUND THEN RAISE EXCEPTION 'Customer % cannot be found!', user; END IF; IF debit::char(5) = user AND credit = 'out' THEN UPDATE gwb set pouch = pouch + amount where acct=user; RAISE INFO 'The % sickles are in your pouch', amount; END IF; RETURN balance; END; $$ LANGUAGE 'plpgsql';