触发不更新表

时间:2017-07-01 01:07:57

标签: oracle

我有这个功能/触发器

CREATE OR REPLACE FUNCTION CARRINHO_SALDO
  (CAR IN Number Default Null)
  RETURN Number
 IS 
  SALD Number; 

BEGIN 
  Select SALDO into SALD from CARRINHO_COMPRA
  Where CONTA = CAR;

  return SALD;

END CARRINHO_SALDO;

create or replace TRIGGER EFETUAR_COMPRA_TRIGGER
  AFTER 
  INSERT OR UPDATE  
  ON EFETUAR_CARRINHO_COMPRA 
  FOR EACH ROW 
  Declare 
  SALD Number;
  BEGIN

  SALD:=CARRINHO_SALDO(:new.CONTA);

  If :new.COMPRA = 'c' then
   SALD := SALD - :new.VALOR;
   else
   SALD := SALD + :new.VALOR;
  End If;

  UPDATE CARRINHO_COMPRA SET SALDO = SALD
    WHERE CONTA = :new.CONTA;
  END;

它的工作方式就像;每次我在" EFETUAR_CARRINHO_COMPRA"表中CARRINHO_COMPRA表必须更新" saldo"。我正确插入

  INSERT INTO EFETUAR_CARRINHO_COMPRA(conta,id_compra,compra,valor,data) VALUES(2,616,'c',1000,'30-06-2017');

表" EFETUAR_CARRINHO_COMPRA"更新确定。但表CARRINHO_COMPRA没有从插入中获取更新的值。

1 个答案:

答案 0 :(得分:0)

如果第一次调用触发器时CARRINHO_COMPRA = CONTA中没有行,则触发器将失败,因为Select SALDO into SALD from CARRINHO_COMPRA中的CARRINHO_SALDO } procedure将抛出NO_DATA_FOUND异常,该异常不会被捕获到任何地方。您可以尝试修改CARRINHO_SALDO以捕获NO_DATA_FOUND例外并合理处理它:

CREATE OR REPLACE FUNCTION CARRINHO_SALDO
    (CAR IN Number Default Null)
  RETURN Number
IS 
  SALD Number; 
BEGIN 
  BEGIN
    Select SALDO
      into SALD from CARRINHO_COMPRA
      Where CONTA = CAR;
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      SALD := 0;
  END;

  return SALD;
END CARRINHO_SALDO;

祝你好运。