PostgreSQL函数中没有可用参数

时间:2019-03-26 19:42:45

标签: postgresql function triggers

我有使用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上的某些填充不是必须插入的,例如INSERTnama_petugas,这是一个临时值。有时会插入,但有时不会。它给出了这样的错误:

nip_petugas

在没有为ERROR: record "new" has no field "nama_petugas" nama_petugas插入值/变量的情况下,如何将其留空?

这是我的触发功能;

nip_petugas

1 个答案:

答案 0 :(得分:1)

只有在定义触发器的表具有该名称的列的情况下,变量NEW.nama_petugas存在。

如果某些表中没有该名称的列,则不能对不同的表使用此触发器。

要么编写几个触发器函数,要么使用this answer中的技巧来使用动态SQL。