-2

I want to create a trigger that catches INSERT and UPDATE and based on the action performs something. In Oracle it can be done doing:

CREATE OR REPLACE TRIGGER ABC_BIU BEFORE INSERT OR UPDATE ON ABC
FOR EACH ROW
BEGIN
  IF INSERTING THEN
    ...
  END IF;

 ...
END;

Here is mentioned that INSERTING is a keyword used in conjunction with the TRIGGER in Oracle and here you can see the usage:

IF INSERTING THEN ... END IF;
IF UPDATING THEN ... END IF;
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Kamil
  • 1,456
  • 4
  • 32
  • 50
  • 4
    `TG_OP` see the manual: http://www.postgresql.org/docs/current/static/plpgsql-trigger.html –  Feb 23 '16 at 10:11

1 Answers1

5

First of all, you need a separate trigger function in Postgres:

CREATE OR REPLACE FUNCTION trg_abc_biu()
  RETURNS trigger AS
$func$ 
BEGIN  
   CASE TG_OP           -- to fork depending on operation
   WHEN ' INSERT' THEN
      -- do something
   WHEN ' UPDATE' THEN
      -- do something
   ELSE
      RAISE EXCEPTION 'This trigger function expects INSERT or UPDATE!';
   END CASE;

   RETURN NEW;
END
$func$  LANGUAGE plpgsql;

... which can then be used in a trigger (or several):

CREATE TRIGGER abc_biu
BEFORE INSERT OR UPDATE ON abc
FOR EACH ROW EXECUTE PROCEDURE trg_abc_biu();

Related answer with more details:

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228