我有一个名为base
的表,我需要在插入记录后更新。我写了一个函数。
CREATE OR REPLACE FUNCTION host_ip() RETURNS trigger AS $host_ip$
BEGIN
update base set thumbnail_url =
replace ("thumbnail_url",'localhost','myipadd') WHERE id = NEW.id;
RETURN NEW;
END;
$host_ip$ LANGUAGE plpgsql;
此功能在插入记录时给出错误。我有一个触发器,在插入后调用该函数。错误为PL/pgSQL function host_ip() line 4 at SQL statement SQL statement
触发器是:
CREATE TRIGGER host_ip AFTER INSERT OR UPDATE ON base
FOR EACH ROW EXECUTE PROCEDURE host_ip();
答案 0 :(得分:1)
要更改刚插入或更新的行的列,只需指定该值即可。无需运行update
。
CREATE OR REPLACE FUNCTION host_ip() RETURNS trigger
AS
$host_ip$
BEGIN
new.thumbnail_url := replace(thumbnail_url,'localhost','myipadd');
RETURN NEW;
END;
$host_ip$
LANGUAGE plpgsql;
但是您无法更改AFTER触发器中的值,您需要BEFORE
触发器:
CREATE TRIGGER host_ip
BEFORE INSERT OR UPDATE ON base
FOR EACH ROW EXECUTE PROCEDURE host_ip();