ORACLE SQL触发器和SUM()

时间:2017-04-18 22:52:22

标签: sql oracle plsql

我有一个表COMMANDE和一个表REGROUPE。我有这个功能:

var lines = `NAME: joe
ACTION: running
NAME: paul
ACTION: walking
ACTION: swimming
NAME: mary
NAME: joe
ACTION: sleeping
NAME: paul
NAME: mary
NAME: ken
ACTION: jumping
ACTION: running
ACTION: eating`


var entries = [];
var current = null;
lines.split("\n").forEach(function(entry) {

    if (entry.indexOf('NAME:') > -1) {

      var name = entry.substring(6);

      // if the current instance hasn't been set yet,
      // or there is already a value present, start a new object.
      if (!current || current.action) {
        current = { name: name, action: null };
        entries.push(current);
        return;
      }

      // append the new name the current name list
      current.name = `${current.name} ${name}`;
      return;
    }

    // append the new action to the current action list. 
    // if action is null, just set the action as its initial value.
    var action = entry.substring(8);
    current.action = !current.action ? action : `${current.action} ${action}`;
});

console.log(entries);

我正在尝试使用该功能来触发,以使用可包含多个项目的命令来更新总价格。我尝试了这个,但它不想工作:

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

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

这个怎么样?我从我的iPhone发送了这个;此代码尚未经过测试。

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;

此代码无效。这将导致突变的触发错误。在触发器内,您无法查询或修改触发器所基于的同一个表,直到触发器完成执行。

ORA-04091:表名发生变异,触发/功能可能看不到它

您现在的问题是,当触发器尝试插入/更新同一个表时,您正在查询regroupe表。这是无法实现的。

您需要找到一种方法来获取prixRegroup列的总和。如果我想到明天我会做出的任何回应都会迟到。