Skip to content

Instantly share code, notes, and snippets.

@Samuyi
Created January 8, 2019 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Samuyi/97f84889c05121aa8c4867a04b334187 to your computer and use it in GitHub Desktop.
Save Samuyi/97f84889c05121aa8c4867a04b334187 to your computer and use it in GitHub Desktop.
a stored function for a trigger
CREATE FUNCTION account_audit_func()
RETURNS TRIGGER
AS $$
BEGIN
IF TG_OP = 'INSERT' THEN
INSERT INTO account_audit (operation, account_id, account_name, debt, balance) VALUES
(TG_OP, NEW.*);
RETURN NEW;
ELSIF TG_OP = 'UPDATE' THEN
INSERT INTO account_audit (operation, account_id, account_name, debt, balance) VALUES
(TG_OP, NEW.*);
RETURN NEW;
ELSIF TG_OP = 'DELETE' THEN
INSERT INTO account_audit (operation, account_id, account_name, debt, balance) VALUES
(TG_OP, OLD.*);
RETURN OLD;
END IF;
END;
$$
LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment