Skip to content

Instantly share code, notes, and snippets.

@MartinThoma
Last active October 10, 2021 09:19
Show Gist options
  • Save MartinThoma/a5dfda185032d2e65521f57a79a19cc5 to your computer and use it in GitHub Desktop.
Save MartinThoma/a5dfda185032d2e65521f57a79a19cc5 to your computer and use it in GitHub Desktop.
CREATE OR REPLACE FUNCTION
transfer_out(source VARCHAR, amount INT) RETURNS void AS $$
DECLARE
source_balance_after int;
BEGIN
UPDATE balances SET balance = balance - amount WHERE id = source;
SELECT balance FROM balances WHERE id = source INTO source_balance_after;
IF source_balance_after < 0 THEN
RAISE EXCEPTION 'Account % does not have enough money', source;
END IF;
END
$$
LANGUAGE 'plpgsql' ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment