我正在PostgreSQL中设置触发器,我不确定如何确定某些情况。例如,我设置了表胶片,并且某些端点更改了在更改前行进行编辑时应创建精确副本的任何部分,并将其保存到films_log。而且,如果几行更改,我也需要这些行也记录下来。
CREATE TABLE films (
id integer PRIMARY KEY,
title varchar(40) NOT NULL,
did integer NOT NULL,
date_prod date,
kind varchar(10)
);
CREATE TABLE films_log (
serial integer,
title varchar(200),
did integer,
date_prod date,
kind varchar(200)
);
CREATE TRIGGER films_logging_trigger
BEFORE UPDATE ON myschema.films
FOR EACH ROW EXECUTE PROCEDURE films_logging();
我尝试过的事情:
CREATE OR REPLACE FUNCTION films_logging() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO films_log (id, title, did, date_prod, kind)
SELECT id, title, did, date_prod, kind FROM films WHERE id = OLD.id
END;
$$;
答案 0 :(得分:0)
Trigger functions have to return a row. In a before
trigger, that is usually the new
row.
You also don't need the SELECT
, using the values from the old
row directly is more efficient:
CREATE OR REPLACE FUNCTION films_logging() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO films_log (id, title, did, date_prod, kind)
values (old.id, old.title, old.did, old.date_prod, old.kind);
return new; --<< here
END;
$$;