什么是 PostgreSQL 等同于 Oracle 的 INSERTING?

What is PostgreSQL equivalent of INSERTING from Oracle?

我想创建一个触发器来捕获 INSERTUPDATE 并根据操作执行某些操作。在 Oracle 中可以这样做:

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可以看到用法:

IF INSERTING THEN ... END IF;
IF UPDATING THEN ... END IF;

首先,你需要在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;

... 然后可以在 触发器 (或多个)中使用:

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

更详细的相关回答:

  • Can a function detect the trigger event type?