PL / SQL触发器返回缺货错误:使用编译错误创建的触发器

WT86

我有以下表格:

order_lines table
    Name  Null?  Type  
    ORDER_ID  NOT NULL  NUMBER(5)  
    PRODUCT_ID  NOT NULL  NUMBER(4)  
    ACTUAL_PRICE     NUMBER  
    DISCOUNT     NUMBER  
    QUANTITY     NUMBER  
    TOTAL_AMOUNT     NUMBER  

Product_inventory table
Name  Null?  Type  
PRODUCT_ID  NOT NULL  NUMBER(4)  
QTY_ON_HAND     NUMBER  
QTY_ON_ORDER     NUMBER  
DATE_ORDERED     DATE  
DELIVERY_DATE     DATE 

我正在尝试使用一个触发器来检查订单数量,看看是否有库存,并显示一条消息。我当前的触发器正在编译,但有一些错误:

    CREATE OR REPLACE TRIGGER check_order_line  
  BEFORE INSERT OR UPDATE ON order_lines
  for each row
DECLARE
  l_current_stock product_inventory.qty_on_hand%type;
BEGIN 
  select product_inventory.qty_on_hand
    into l_current_stock
    from product_inventory, order_lines
   where product_inventory.product_id = :new.product_id;
  if(:new.quantity > l_current_stock) then
    RAISE_APPLICATION_ERROR(-20103, 'Insufficient Stock');
  else
    update product_inventory
   set qty_on_hand = qty_on_hand - :new.quantity
 where product_inventory.product_id = :new.product_id;
  end if;
END;  

在测试触发器时,出现以下错误:

 insert into order_lines values (388,1023,100,20,2,160)
            *

ERROR at line 1: 
ORA-01422: exact fetch returns more than requested number of rows 
ORA-06512: at "DBA643.CHECK_ORDER_LINE", line 4 
ORA-04088: error during execution of trigger 'DBA643.CHECK_ORDER_LINE' 

我为什么得到这个?

a_horse_with_no_name

在赋值操作符SQL=,不:=(只对PL / SQL)。另外,比较运算符也是=,不是:=

所以这条语句:

update product_inventory
   set qty_on_hand:= qty_on_hand - :new.quantity
 where product_id:= :new.product_id;

应该

update product_inventory
   set qty_on_hand = qty_on_hand - :new.quantity
 where product_id = :new.product_id;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章