Sql postgres中的触发器
我用PL/pgSQL为PostgreSQL编写了一个简单的触发器,但它并没有按照我的计划工作。我有两个问题:Sql postgres中的触发器,sql,postgresql,triggers,plpgsql,Sql,Postgresql,Triggers,Plpgsql,我用PL/pgSQL为PostgreSQL编写了一个简单的触发器,但它并没有按照我的计划工作。我有两个问题: 是否有PostgreSQL函数的调试器 我错在哪里?我需要在这里检查idprod 如果idprod不是空的,那么您的if(idprod>0)可能最好写成,但是idprod>0应该可以工作 我猜错误就在这里: update warehouses set amount = whamount * new.amount; 您在该更新中没有WHERE子句,因此您将更新表中的每一行。您可以使用此
idprod
如果idprod不是空的,那么您的
if(idprod>0)
可能最好写成,但是idprod>0
应该可以工作
我猜错误就在这里:
update warehouses set amount = whamount * new.amount;
您在该更新中没有WHERE子句,因此您将更新表中的每一行。您可以使用此选项:
select amount into whamount from warehouses where product = idprod;
update warehouses set amount = whamount * new.amount where product = idprod;
或者更好,只需一次更新:
update warehouses set amount = amount * new.amount where product = idprod;
如果使用后者,那么在DECLARE部分就不再需要whamount
另外,我认为您可以用一个选择来替换前两个选择。这两个:
SELECT PRODUCT INTO idsupp from SUPPLIERS where ID = NEW.SUPPLIER;
SELECT ID INTO idprod from PRODUCTS where ID = idsupp;
可以替换为以下内容:
select p.id into idprod
from products p
join suppliers s on p.id = s.product
where s.id = new.supplier;
然后,您也不需要idsupp
就调试而言,我只知道printf
风格的调试;例如:
raise notice 'idprod is "%"', idprod;
对于调试,您还可以执行以下操作:
RAISE DEBUG 'some debug message and variable value: %', some_variable;
然后可以将日志级别设置为debug以输出调试。您可以保留这些调试语句,以便在将来进行调查时,这些调试语句可能仍然有用。感谢您的回答。我在另一个地方犯了错误,已经找到了。我将查阅您的调试建议。
RAISE DEBUG 'some debug message and variable value: %', some_variable;