Skip to content

Instantly share code, notes, and snippets.

@Samuyi
Created January 8, 2019 16:43
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