当其他一些列发生变化时,我试图计算一个列上的值,所以我创建了一个这样的触发器。我的“total_points”列总是包含 0,所以有些不对劲。我做错了什么?我验证了下面的每个 SELECT 调用都返回一个非零值。
CREATE FUNCTION update_order_total_points() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
NEW.total_points = NEW.ud * (SELECT amount FROM points WHERE abbrev = 'ud') +
NEW.dp * (SELECT amount FROM points WHERE abbrev = 'dp') +
NEW.swrv * (SELECT amount FROM points WHERE abbrev = 'swrv') +
NEW.sh * (SELECT amount FROM points WHERE abbrev = 'sh') +
NEW.jmsw * (SELECT amount FROM points WHERE abbrev = 'jmsw') +
NEW.sw * (SELECT amount FROM points WHERE abbrev = 'sw') +
NEW.prrv * (SELECT amount FROM points WHERE abbrev = 'prrv') +
NEW.mhsw * (SELECT amount FROM points WHERE abbrev = 'mhsw') +
NEW.bmsw * (SELECT amount FROM points WHERE abbrev = 'bmsw') +
NEW.mp * (SELECT amount FROM points WHERE abbrev = 'mp') +
NEW.pr * (SELECT amount FROM points WHERE abbrev = 'pr') +
NEW.st * (SELECT amount FROM points WHERE abbrev = 'st');
RETURN NEW;
END;
$$;
CREATE TRIGGER fix_total_points
AFTER INSERT OR UPDATE OF ud, dp, swrv, sh, jmsw, sw, prrv, mhsw, bmsw, mp, pr, st
ON orders
FOR EACH ROW EXECUTE PROCEDURE update_order_total_points();