Skip to content

Instantly share code, notes, and snippets.

@ecthiender
Created August 17, 2018 13:47
Show Gist options
  • Save ecthiender/f25e3bd1fe254a0ed20b63b0bc8d1224 to your computer and use it in GitHub Desktop.
Save ecthiender/f25e3bd1fe254a0ed20b63b0bc8d1224 to your computer and use it in GitHub Desktop.
-- Create the function
CREATE FUNCTION insert_deposit()
RETURNS trigger AS $BODY$
DECLARE active_account BOOLEAN;
BEGIN
IF NEW."deposit_amount" <= 0 THEN
RAISE EXCEPTION 'Deposit amount must be greater than 0';
END IF;
SELECT a.is_active INTO active_account FROM "account_savings" a WHERE a.account_no = NEW."account_no";
IF active_account != TRUE THEN
RAISE EXCEPTION 'Account must be active';
END IF;
RETURN NEW;
END;
$BODY$ LANGUAGE plpgsql;
--- Create the trigger with the above function
CREATE TRIGGER insert_deposit BEFORE INSERT OR UPDATE ON "deposit_activity" FOR EACH ROW EXECUTE PROCEDURE insert_deposit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment