我使用PostgreSQL构建了这个函数:
CREATE OR REPLACE FUNCTION "db"."t_pencabutan_likuidasi_after_update"()
RETURNS "pg_catalog"."trigger" AS $BODY$
BEGIN
IF(NEW.status <> OLD.status) THEN
INSERT INTO
t_pencabutan_likuidasi_log(id_pencabutan, id_profile, nama_petugas, nip_petugas, status, catatan)
VALUES(
NEW.id_pencabutan, NEW.id_profile, NEW.nama_petugas, NEW.nip_petugas, NEW.status, NEW.catatan);
END IF;
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100上面是一个函数,它将在更新表的之前触发liquidator。
问题是INSERT上的某些填充不是强制插入的,比如nama_petugas和nip_petugas,这是一个临时值。有时插入,但有时不插入。它会产生如下错误:
ERROR: record "new" has no field "nama_petugas"如果没有为nama_petugas和nip_petugas插入的值/变量,如何将其保留为空白?
这是我的触发功能;
CREATE TRIGGER
"t_pencabutan_likuidasi_after_update"
AFTER UPDATE OF "status"
ON "db"."liquidator"
FOR EACH ROW
EXECUTE PROCEDURE
"db"."t_pencabutan_likuidasi_after_update"();发布于 2019-03-27 07:30:36
变量NEW.nama_petugas存在的当且仅当定义触发器的表具有该名称的列。
如果某些表没有同名的列,则不能将此触发器与不同的表一起使用。
要么编写几个触发器函数,要么像在this answer中那样使用动态SQL。
https://stackoverflow.com/questions/55365119
复制相似问题