我很难让我的'更新后'触发器正常工作。
从我的简单查询中可以看出,production_work的总和与订单元素的总和相匹配。
# select ident,ud,dp,swrv,sh,jmsw,sw,prrv,mhsw,bmsw,mp,pr,st,completed from orders;
ident | ud | dp | swrv | sh | jmsw | sw | prrv | mhsw | bmsw | mp | pr | st | completed
-------+----+----+------+----+------+----+------+------+------+----+----+----+-----------
2 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | f
(1 row)
# select * from production_work;
ident | order_id | producer_id | day | ud | dp | swrv | sh | jmsw | sw | prrv | mhsw | bmsw | mp | pr | st
-------+----------+-------------+------------+----+----+------+----+------+----+------+------+------+----+----+----
5 | 2 | 1 | 2013-08-09 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
6 | 2 | 2 | 2013-08-09 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
(2 rows)
如果工作元素的总和与使用此触发器的order元素匹配,我试图将'completed'设置为true:
CREATE OR REPLACE FUNCTION update_order_completion_status() RETURNS trigger AS
$BODY$
BEGIN
WITH w AS (
SELECT SUM(ud) AS ud, SUM(dp) AS dp, SUM(swrv) AS swrv, SUM(sh) AS sh, SUM(jmsw) AS jmsw, SUM(sw) AS sw, SUM(prrv) AS prrv,
SUM(mhsw) AS mhsw, SUM(bmsw) AS bmsw, SUM(mp) AS mp, SUM(pr) AS pr, SUM(st) AS st
FROM production_work
WHERE order_id = OLD.order_id
), o AS (
SELECT ud, dp, swrv, sh, jmsw, sw, prrv, mhsw, bmsw, mp, pr, st
FROM orders
WHERE ident = OLD.order_id
)
UPDATE orders
SET completed = (w.ud = o.ud AND w.dp = o.dp AND w.swrv = o.swrv AND w.sh = o.sh AND w.jmsw = o.jmsw AND w.sw = o.sw AND
w.prrv = o.prrv AND w.mhsw = o.mhsw AND w.bmsw = o.bmsw AND w.mp = o.mp AND w.pr = o.pr AND w.st = o.st)
WHERE ident = OLD.order_id;
END;
$BODY$ LANGUAGE plpgsql ;
CREATE TRIGGER update_order_completion_status_trigger
AFTER UPDATE OF ud, dp, swrv, sh, jmsw, sw, prrv, mhsw, bmsw, mp, pr, st
ON production_work
FOR EACH ROW EXECUTE PROCEDURE update_order_completion_status();
当我对production_work表进行更新时,我没有收到任何错误消息,但正如您所看到的那样,已完成的列未设置为true。
答案 0 :(得分:0)
触发器函数中的SQL查询不必要地复杂且不正确(FROM
中缺少UPDATE
子句)。简化的正确版本是:
UPDATE orders o
SET completed = (w.ud = o.ud AND w.dp = o.dp AND ...)
FROM (
SELECT SUM(ud) AS ud, SUM(dp) AS dp, ...
FROM production_work
WHERE order_id = NEW.order_id
) w
WHERE o.ident = NEW.order_id
AND o.completed <> (w.ud = o.ud AND w.dp = o.dp AND ...);
假设completed NOT NULL
。
注意,我使用NEW
而非OLD,因为您想使用新值
但是,如果production_work.order_id
可以更改,则必须更新orders
和 OLD
的{{1}}。
最后一行是为了防止空更新。
如果您不需要冗余列NEW
来优化效果,我会考虑改为VIEW:
completed
假设CREATE VIEW order_plus AS
SELECT o.*, (SUM(w.ud) = o.ud AND SUM(w.dp) = o.dp AND ...) AS completed
FROM orders o
JOIN production_work w ON o.ident = w.ident
GROUP BY o.ident;
。