gpt4 book ai didi

postgresql - `instead of update` 触发器 : How to distinguish when user provides new value for the column and when does not?

转载 作者:行者123 更新时间:2023-11-29 13:41:51 25 4
gpt4 key购买 nike

触发器内部 NEW.field值总是有 <some value>尽管该列出现在 SET 上UPDATE 语句列表或不:

UPDATE test SET name = 'abc', field = <some value>;
UPDATE test SET name = 'abc';

对于触发器中的两个查询,我将得到 field<some value> .

有没有办法从内部触发器中区分这两个查询?

最佳答案

我认为您可以将新值与旧值进行比较。如果不同,则表明该字段已设置。

除非你想在字段有更新时捕获,否则即使字段值没有改变,我认为这会捕获它。

CREATE OR REPLACE FUNCTION test_update_trigger()
RETURNS trigger AS
$BODY$
BEGIN

if (OLD.field is null and NEW.field is not null or
OLD.field is not null and NEW.field is null or
OLD.field != NEW.field) then
-- do something
end if;

return NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

关于postgresql - `instead of update` 触发器 : How to distinguish when user provides new value for the column and when does not?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54197959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com