PLSQL - 触发无响应

时间:2014-11-27 22:50:25

标签: sql plsql

我的触发器有问题。触发器的目的是在产品(" Artigo")被放入账单线时更新股票(" STOCK")(" Linha_Fatura_Encomenda& #34;)来自法案(" Fatura_Encomenda")。当我将产品放入账单时,应该验证数量(" QUANTIDADE")以确保库存可以覆盖它,但这不会发生。

create or replace TRIGGER stock_disponivel_check
  BEFORE INSERT OR UPDATE OF QUANTIDADE ON LINHA_FATURA_ENCOMENDA
  FOR EACH ROW

DECLARE
  STOCK   INTEGER;
  Value_out_of_range  EXCEPTION;
  PRAGMA EXCEPTION_INIT (Value_out_of_range, -4096);

BEGIN
  UPDATE ARTIGO 
    SET STOCK = STOCK - :NEW.QUANTIDADE
    WHERE STOCK > 0;

  IF (STOCK = 0 OR STOCK <0) THEN
    RAISE Value_out_of_range;
  END IF;
EXCEPTION
  WHEN Value_out_of_range THEN
    Raise_application_error (
      -20300,
      'Stock '|| TO_CHAR(STOCK) ||' inexistente!'
    );
  WHEN NO_DATA_FOUND THEN
    Raise_application_error(-20322, 'Nenhum Valor Introduzido!');
END;

有什么想法吗? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

尝试重命名本地变量并将STOCK字段的值返回到此变量中(可能只有零行或一行更新,否则会失败)

DECLARE
  L_STOCK   INTEGER;
  Value_out_of_range  EXCEPTION;
  PRAGMA EXCEPTION_INIT (Value_out_of_range, -4096);

BEGIN
  UPDATE ARTIGO 
    SET STOCK = STOCK - :NEW.QUANTIDADE
    WHERE STOCK > 0
  RETURNING STOCK INTO L_STOCK;

  IF (L_STOCK = 0 OR L_STOCK < 0) THEN
    RAISE Value_out_of_range;
  END IF;
EXCEPTION
  WHEN Value_out_of_range THEN
    Raise_application_error (
      -20300,
      'Stock '|| TO_CHAR(L_STOCK) ||' inexistente!'
    );
  WHEN NO_DATA_FOUND THEN
    Raise_application_error(-20322, 'Nenhum Valor Introduzido!');
END;