有条件地在插入触发器之前删除插入而不返回错误

Conditionally drop an insert in a before insert trigger without returning error

我在插入前触发器中有以下功能:

CREATE OR REPLACE FUNCTION schema.table_somefun()
RETURNS trigger AS
LANGUAGE 'plpgsql';
$BODY$
BEGIN
  IF NEW.col2 NOT NULL THEN
    NEW.col1 := CASE NEW.col1
                     WHEN '121432' THEN '321123'
                     ELSE <command> END CASE; --there should be a command aborting insertion without error or exception
  END IF;
  RETURN NEW;
END;
$BODY$

ELSE 语句应该中止插入。是否有一个命令可以在不告诉客户端的情况下删除查询并保持 table 不变?

就用

RETURN NULL;

而不是

RETURN NEW;

取消该行的 INSERT 并且什么也不做。

但是您不能在 SQL CASE 表达式中执行 PL/pgSQL 语句 。 (不要混淆SQL CASE with the similar control structure CASE of PL/pgSQL!)
可能看起来像这样:

CREATE OR REPLACE FUNCTION schema.table_somefun()
  RETURNS trigger AS
$func$
BEGIN
   IF NEW.col2 NOT NULL THEN
      IF NEW.col1 = '121432' THEN  -- could also be plpgsql CASE ...
         NEW.col1 := '321123';
      ELSE
         RETURN NULL;
      END IF;
  END IF;
  RETURN NEW;
END
$func$  LANGUAGE plpgsql;