我有使用PostgreSQL构建的此功能:
rand
上述功能将触发表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
答案 0 :(得分:1)
只有在定义触发器的表具有该名称的列的情况下,变量NEW.nama_petugas
存在。
如果某些表中没有该名称的列,则不能对不同的表使用此触发器。
要么编写几个触发器函数,要么使用this answer中的技巧来使用动态SQL。