【发布时间】:2021-02-28 21:47:26
【问题描述】:
我想排除某些列在更新时触发触发器,我尝试了此处显示的代码的几种变体,但当processed_on 在更新前不为空时,它不起作用(不更新import_timestamp)。
我尝试添加这个注释掉的 OR 子句,但还是一样。有什么想法吗?
预期的行为是在除processed_on 之外的任何列上更新import_timestamp,而不是在processed_on 更新时更新它(processed_on 更新不可能发生在与其他列相同的事务中)。
create table test
(
id serial,
some_value integer,
import_timestamp timestamp,
processed_on timestamp
);
insert into test(some_value, import_timestamp, processed_on)
values (1312, '2021-02-02 20:00:00', '2021-02-02 20:44:00'),
(124, '2021-02-02 20:10:00', null),
(500, '2021-02-02 20:20:00', null);
CREATE OR REPLACE FUNCTION trg_update_import_timestamp (
)
RETURNS trigger AS
$body$
BEGIN
IF OLD.processed_on = NEW.processed_on
--OR (OLD.processed_on = null AND NEW.processed_on = null)
THEN NEW.import_timestamp = now();
ELSE
NEW.import_timestamp = OLD.import_timestamp;
END IF;
RETURN NEW;
END;
$body$
LANGUAGE 'plpgsql';
CREATE TRIGGER test_update_trg
BEFORE UPDATE
ON test
FOR EACH ROW
EXECUTE PROCEDURE trg_update_import_timestamp();
update test set some_value = 444 where id in (1,3);
select * from test order by 1
【问题讨论】:
标签: sql postgresql triggers