如何解决突变触发

时间:2018-06-27 00:32:13

标签: oracle plsql triggers mutating-table

我在PL / SQL ORACLE中是新手 我有一个问题,想要为一张可变桌子触发

我想实现以下目标:通过在ORDER_ITEMS表格上插入订单,将已订购的金额(数量)保留在表格库存(QUANTITY_ON_HAND)中

TABLE ORDER_ITEMS
-ORDER_ID
-PRODUCT_ID-
-UNIT_PRICE
-QUANTITY

密钥是ORDER_ID,并按顺序增加

THE TABLE INVENTORIES
-PRODUCT_ID
-WHAREHOUSE_ID
-QUANTITY_ON_HAND

我执行的功能获得了表ORDER_ITEMS中插入的最后一个记录的PRODUCT_ID和有序数量

CREATE OR REPLACE FUNCTION get_data__order_quantity
RETURN number IS
quantity_order number;
BEGIN
 select quantity into quantity_order from (select * from ORDER_ITEMS order by order_id desc) where rownum=1;
 RETURN quantity_order ;
END get_data__order_quantity;

CREATE OR REPLACE FUNCTION get_data_cod_prod
RETURN number IS
cod_producto number;
BEGIN
 select product_id into cod_producto from (select * from ORDER_ITEMS order by order_id desc) where rownum=1;
 RETURN cod_producto ;
END get_data_cod_prod;

我已经执行过此表格的触发操作

create or replace trigger actualizar_stock
after insert  --AFTER INSERT
on ORDER_ITEMS  -- ON TABLE ORDER_ITEMS
for each row

declare
 ctn_ordenada int;  --STORE THE AMOUNT OF ORDERED PRODUCTS
 cod_produc int;    --STORE THE ID OF THE PRODUCT THAT HAS BEEN ORDERED
 id_wharehouse int; --STORE THE ID OF THE FIRST WAREHOUSE ENCOUNTERED WITH THE PRODUCT IN EXISTENCE
 stock_bodega int;  --STORE THE EXISTENCE OF PRODUCTS (STOCK) IN BODEGA

begin
  ctn_ordenada:=get_data__order_quantity; --THE AMOUNT ORDERED FROM THE FUNCTION IS ASSIGNED
  cod_produc :=get_data_cod_prod;         -- THE PRODUCT ID IS ASSIGNED FROM THE FUNCTION

  select quantity_on_hand into stock_bodega   from inventories where (quantity_on_hand > 0 and rownum=1 and product_id=cod_produc); -- SE ASIGNA EL STOCK
  select  wharehouse_id into  id_wharehouse  from inventories where (quantity_on_hand > 0 and rownum=1 and product_id=cod_produc);  -- THE ID OF THE WAREHOUSE IS ASSIGNED

 -- IF THE ESTOCK IS GREATER THAN 0
 if (stock_bodega >0) then
 begin
 --UPDATE (REST THE CURRENT VALUE WITH THE ORDERED AMOUNT)
 update inventories set quantity_on_hand=quantity_on_hand-ctn_ordenada where wharehouse_id=id_wharehouse;
 end;
 else
  begin
  -- DO NOT UPDATE
  dbms_output.put_line('Registration not updated');
  end;
  end if;
end;

-- END OF THE TRIGGER

请帮助

1 个答案:

答案 0 :(得分:0)

基本问题是从触发器中调用的函数get_data__order_quantityget_data_cod_prod访问定义了触发器的表。这将导致变异表异常。

另一个问题,当然是,您的函数很可能无法实现您的预​​期。您说“我已执行的功能要获得在表ORDER_ITEMS中插入的最后一个记录的PRODUCT_ID和订购的金额”,但是您的选择标准是...WHERE ROWNUM = 1。无法提前知道该记录是什么-在没有特定选择或排序标准的情况下,数据库可以按其关心的任何顺序自由返回行。它可能返回您插入的最后一行-但它可能返回表中的任何随机行。您根本无法根据ROWNUM = 1知道将表的最后一行插入。

幸运的是,您无需执行任何操作,因为您已经可以访问刚刚通过:NEW伪行插入的行。我建议将触发器重写为:

create or replace trigger actualizar_stock
  after insert on ORDER_ITEMS
  for each row
begin
  --UPDATE (REST THE CURRENT VALUE WITH THE ORDERED AMOUNT)

  update inventories
    set quantity_on_hand = quantity_on_hand - :NEW.QUANTITY
    where QUANTITY_ON_HAND > 0 AND
          product_id = :NEW.PRODUCT_ID;

  IF SQL%ROWCOUNT = 0 THEN
    -- No rows updated
    dbms_output.put_line('Registration not updated');
  end if;
end actualizar_stock;

好运。