CREATE or REPLACE PROCEDURE UPDATE_SUBTOTAL is
v_order_no orderline.order_no%type;
v_subtotal number(15);
CURSOR product_orderline_cur is
SELECT ol.order_no, sum(p.unit_price * ol.qty) as subtotal
from product p, orderline ol
where p.product_no = ol.product_no
group by ol.order_no;
BEGIN
OPEN product_orderline_cur;
LOOP
FETCH product_orderline_cur into v_order_no, v_subtotal;
EXIT when product_orderline_cur%notfound;
-- store subtotal in orders table
UPDATE orders
SET subtotal = v_subtotal
WHERE order_no = v_order_no;
END LOOP;
--an order may be created but no orderlines added yet,insert a 0
UPDATE orders
SET subtotal = 0
WHERE subtotal is null;
CLOSE product_orderline_cur;
END;
/
show errors;