简体   繁体   中英

ORACLE SQL trigger and SUM()

I have a table COMMANDE and a table 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; 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

Your problem right now is that you're querying the regroupe table while the trigger is trying to insert/update the same table. This cannot be accomplished.

You'll need to find a way to get the sum of the prixRegroup column some other way. It's late if I think of anything I'll respond tomorrow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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