我正在尝试在触发器函数中执行动态查询,但我不断获得Missing FROM-clause entry for table "new"
为什么会出现错误以及如何解决?
CREATE OR REPLACE FUNCTION "Site"."UpdateAncestorModified"()
RETURNS trigger AS
$BODY$BEGIN
EXECUTE
format
('
UPDATE
"' || TG_TABLE_SCHEMA || '"."' || TG_TABLE_NAME || '"
SET
modified = NEW.modified
WHERE
id = NEW."ancestorId"
AND
modified <> NEW.modified
')
USING
NEW;
RETURN NEW;
END$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
答案 0 :(得分:2)
我必须将NEW
更改为($1)
,因为它是对第一个绑定参数的引用。
正确的查询是:
CREATE OR REPLACE FUNCTION "Site"."UpdateAncestorModified"()
RETURNS trigger AS
$BODY$BEGIN
EXECUTE
format
('
UPDATE
"' || TG_TABLE_SCHEMA || '"."' || TG_TABLE_NAME || '"
SET
modified = ($1).modified
WHERE
id = ($1)."ancestorId"
AND
modified <> ($1).modified
')
USING
NEW;
RETURN NEW;
END$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;