Skip to content

Instantly share code, notes, and snippets.

@dxdinh
Forked from Samuyi/audit_trigger.sql
Created September 1, 2020 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dxdinh/b992bff1031a94bb9e247cc00df09dc0 to your computer and use it in GitHub Desktop.
Save dxdinh/b992bff1031a94bb9e247cc00df09dc0 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