Reputation: 1799
I have mostly the same code for a trigger that occurs before a INSERT
or UPDATE
,
and instead of creating 2 triggers, is there any way to know the type of event occurring dynamically inside the trigger ?
Example:
CREATE OR REPLACE FUNCTION UpsertClientLog() RETURNS TRIGGER AS $$
DECLARE action_type VARCHAR(255);
BEGIN
IF (event == 'INSERT') THEN
action_type = 'Create'
ELSE
action_type = 'Update'
END IF;
-- Same code from here on out ..
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER UPSERT_CLIENT_LOG BEFORE INSERT OR UPDATE ON client
FOR EACH ROW EXECUTE PROCEDURE UpsertClientLog();
Upvotes: 0
Views: 221
Reputation: 222432
You can use special variable TO_OP
:
TG_OP
Data type text; a string of
INSERT
,UPDATE
,DELETE
, orTRUNCATE
telling for which operation the trigger was fired.
Upvotes: 1