0

我有两个表Orderitems:

"ORDER_ITEMS_CODE" VARCHAR2(20) NOT NULL ENABLE, 
"ORDER_CODE" VARCHAR2(20) NOT NULL ENABLE, 
"ITEM_CODE_ORDERS" VARCHAR2(20) NOT NULL ENABLE, 
"ORDER_QUANTITY" NUMBER(4,0) NOT NULL ENABLE, 
"ORDER_UNIT" VARCHAR2(5) NOT NULL ENABLE, 
"UNIT_PRICE" NUMBER(38,5), 
"ORDERED_IN" VARCHAR2(6), 
"OR_QUANTITY_TON" NUMBER(38,5),
"Warehouse_CODE" VARCHAR2(20) NOT NULL ENABLE

另一个表是库存:

"INVENTORY_CODE" VARCHAR2(20) NOT NULL ENABLE, 
"ITEM_CODE" VARCHAR2(20) NOT NULL ENABLE, 
"WAREHOUSE_CODE" VARCHAR2(20),  
"IN_Q_TON" NUMBER(38,5), 
"OR_Q_TON" NUMBER(38,5) 

我创建了一个触发器来计算 "OR_QUANTITY_TON" = Sum ("OR_QN_TON")

create or replace trigger sum_Or_IT
after insert or update or delete on orderitems
begin
    update INVENTORY set OR_Q_TON = (
        select sum(or_quantity_ton) from orderitems 
        where
        orderitems.item_code_orders = INVENTORY.item_code
        and
        warehouse_code = '1');
end;

这是一个库存表,列 OR_Q_TON 是每个项目的订购数量的总和,但是如果订单中不存在某个项目,我希望订单数量为零。如果表“Orderitems”中不存在“item_code_orders”,我想例外,那么 OR_Q_TON = 0

我试过了,但没有用我得到(-)不是(0)

EXCEPTION
  WHEN NO_DATA_FOUND THEN
    update INVENTORY set OR_Q_TON = 0 ;
4

3 回答 3

0

它也不例外,如果您需要使用上述更新,AND oracle 不会引发异常sql%rowcount

尝试:

IF SQL%ROWCOUNT = 0 THEN
  update INVENTORY set OR_Q_TON = 0 ;
END IF;
于 2013-08-26T08:37:14.100 回答
0

您可以使用它而不是使用 EXCEPTION,

IF SQL%NOTFOUND THEN 
    UPDATE inventory SET or_q_ton = 0 ;
    COMMIT;
END IF;
于 2013-08-26T08:38:27.540 回答
0

触发器应该是

create or replace trigger sum_Or_IT
after insert or update or delete  on orderitems
begin
    update INVENTORY set OR_Q_TON = (
        select coalesce(sum(or_quantity_ton),0) from orderitems 
        where
        orderitems.item_code_orders = INVENTORY.item_code
        and
        warehouse_code = '1')        
end;

通过使用合并函数而不是异常解决的问题

于 2013-08-28T14:08:28.503 回答