Last active
October 10, 2021 09:19
-
-
Save MartinThoma/a5dfda185032d2e65521f57a79a19cc5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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