我是 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
我已经执行了获得 PRODUCT_ID 和插入到表 ORDER_ITEMS 中的最后一条记录的订购量的功能
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
请帮忙