我有三张桌子
produit(pid,nom,qteinitial,qteinstock,prix),
prdouitinbondachat(pid,baid,qte,prix),
prdoutinbondevente(pid,bvid,qte,prix) ,
我必须计算我的股票中的项目数量。所以,方程式shoubld。 qtyIntial + IN - OUT。
我已经定义了一个触发器来做到这一点。但我似乎,我有一个问题。 这是我对触发器的声明:
CREATE OR REPLACE FUNCTION updateProduitQte() RETURNS TRIGGER AS $example_table$
BEGIN
IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN
UPDATE produit set qtestock= ((select coalesce(sum(qte), 0) from prdouitinbondachat where pid = new.pid)
+ (select coalesce((qteinitial), 0) from produit where pid = new.pid) -
(select coalesce(sum(qte), 0) from prdouitinbondevente WHERE pid = new.pid)) ;
return NEW ;
ELSIF (TG_OP = 'DELETE') THEN
UPDATE produit set qtestock=
((select coalesce(sum(qte), 0) from prdouitinbondachat where pid = old.pid)
+ (select coalesce((qteinitial), 0) from produit where pid = old.pid) -
(select coalesce(sum(qte), 0) from prdouitinbondevente WHERE pid = old.pid)) ;
return OLD ;
END IF;
RETURN NULL;
END;
$example_table$ LANGUAGE plpgsql;
这里我将功能与触发器匹配:
//vente triggeer
CREATE TRIGGER venteTrigger AFTER INSERT OR UPDATE OR DELETE ON prdouitinbondevente
FOR EACH ROW EXECUTE PROCEDURE updateProduitQte();
//achat trigger
CREATE TRIGGER achatTrigger AFTER INSERT OR UPDATE OR DELETE ON prdouitinbondachat
FOR EACH ROW EXECUTE PROCEDURE updateProduitQte();