4

我有一个触发器功能,我只想在某些 INSERTS 实例上触发,在这种情况下,if do_backup = true. 如果它在所有情况下都触发,我会得到一个无限循环。逻辑对我来说似乎很简单,其余的功能都有效。但是触发功能似乎没有注册我的条件并且总是运行,即使备份 = true。

CREATE OR REPLACE FUNCTION table_styles_backup() RETURNS
 TRIGGER AS $table_styles_backup$

 DECLARE
 ...
 do_backup  boolean;

    BEGIN
 SELECT backup INTO do_backup FROM table_details WHERE id=NEW.table_meta_id;

        IF (do_backup = true) THEN 

  ...


            INSERT INTO table_styles_versions 
   (
   ...
   )

  VALUES (
   ...
   );
 END IF;

    RETURN NULL;

    END;

$table_styles_backup$  LANGUAGE plpgsql;

CREATE TRIGGER table_styles_backup AFTER INSERT ON table_styles

FOR EACH ROW EXECUTE PROCEDURE table_styles_backup();
4

1 回答 1

5

根据文档,您无需在 IF 语句中显式测试布尔数据类型 - 它们默认测试是否为真,因此您可以使用:

IF do_backup THEN
  ...
  INSERT INTO table_styles_versions (...
ELSE
  -- omit this & the ELSE keyword if nothing needs to happen if do_backup is false
END IF;
于 2010-06-16T20:07:49.443 回答