简体   繁体   English

ORACLE SQL触发器和SUM()

[英]ORACLE SQL trigger and SUM()

I have a table COMMANDE and a table REGROUPE. 我有一个表COMMANDE和一个表REGROUPE。 I have that function: 我有那个功能:

CREATE OR REPLACE PROCEDURE multiplicateur(a NUMBER, taxes NUMBER, c OUT NUMBER)
IS
BEGIN
    c := a * taxes ;
END multiplicateur;
/

I'm trying to make a trigger using that founction to update a total price with taxes from a command that can contain more than one item. 我正在尝试使用该功能来触发一个触发器,以通过一个可以包含多个项目的命令来更新含税的总价。 I tried this but it doesn't want to work: 我尝试了这个,但是它不想工作:

create or replace TRIGGER MAJ_PRIX_COMMANDE
AFTER INSERT OR UPDATE OR DELETE ON REGROUPE
FOR EACH ROW
DECLARE 
resultat NUMBER; 
BEGIN
UPDATE COMMANDE
SET COMMANDE.prixTotal = multiplicateur((CAST((SELECT SUM(prixRegroupe)FROM REGROUPE WHERE REGROUPE.numCommande = :NEW.numCommande)AS NUMBER)),1.15,resultat)

WHERE COMMANDE.numCommande = :NEW.numCommande;
END;

Can someone help me? 有人能帮我吗?

How about this? 这个怎么样? I sent this from my iPhone; 我是从iPhone发送的; this code hasn't been tested. 此代码尚未经过测试。

create or replace TRIGGER MAJ_PRIX_COMMANDE
AFTER INSERT OR UPDATE OR DELETE ON REGROUPE
FOR EACH ROW
DECLARE 
 resultat NUMBER; 
 l_groupe NUMBER; --will store sum based on numCommande
BEGIN
 --retrieve sum of prixRegroupe
 SELECT SUM(r.prixRegroupe)
   INTO l_groupe
   FROM regroupe r
  WHERE r.numCommande = :NEW.numCommande;

  --call procedure and pass the sum of priRegroupe
  multiplicateur(l_groupe, 1.15, resultat);

  --use precedures out argument to update commande
 UPDATE COMMANDE c
    SET c.prixTotal = resultat
  WHERE c.numCommande = :NEW.numCommande;
END;

This code won't work. 此代码无效。 It will result in a mutating trigger error. 这将导致变异的触发错误。 Within a trigger you cannot query or modify the same table that the trigger is based on until after the trigger has completed execution. 在触发器内,您无法查询或修改触发器所基于的同一表,直到触发器完成执行。

ORA-04091: table name is mutating, trigger/function may not see it ORA-04091:表名正在变异,触发器/函数可能看不到它

Your problem right now is that you're querying the regroupe table while the trigger is trying to insert/update the same table. 现在的问题是,当触发器尝试插入/更新同一表时,您正在查询regroupe表。 This cannot be accomplished. 这是无法实现的。

You'll need to find a way to get the sum of the prixRegroup column some other way. 您将需要找到其他方法来获取prixRegroup列的总和。 It's late if I think of anything I'll respond tomorrow. 如果我想到明天要回应的话,已经太晚了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM