Question

I need to create a trigger that updates the quantity in the Product table whenever an order of type 2 from Order_call table is described in the Order_line table. I have tried the below trigger but I keep getting errors:

CREATE OR REPLACE TRIGGER updqty AFTER INSERT OR UPDATE ON ORDER_LINE FOR EACH ROW 
DECLARE
ortype NUMBER(1);
BEGIN 
SELECT @ortype=type FROM ORDER_CALL WHERE orderID=NEW.orderID;
    IF (ortype=2) THEN
    UPDATE PRODUCT SET qty = qty - NEW.qty WHERE part_no=NEW.part_no;
END IF;
END;

I have the Products table than contains the quantity of each available product, the Order_call table that registers the orderID and its type (type 2 means it's an actual sale) and the Order_Line table where the actual order is detailed. It stores on rows the orderID, the part_no, and the quantity sold. So, for each orderID of type 2 from the Order_Call table I need to take the quantity of each product sold (from Order_Line) and subtract it from the qty of the respective part_no in the Product table.

I am receiving the following compilation errors:

4/2  PL/SQL: SQL Statement ignored
4/19 PL/SQL: ORA-00936: missing expression

I don't have too much SQL experience. Thanks for helping.

Was it helpful?

Solution

I finally managed to get it, if anyone else would need this later on:

  CREATE OR REPLACE TRIGGER updqty
   BEFORE INSERT OR UPDATE
   ON ORDER_LINE
   FOR EACH ROW
DECLARE
   ortype   NUMBER (5);
BEGIN
   SELECT TYPE
     INTO ortype
     FROM ORDER_CALL
    WHERE order_call.orderID = :NEW.orderID;

   IF (ortype = 2)
   THEN
      UPDATE PRODUCT
         SET qty = qty - :NEW.qty
       WHERE part_no = :NEW.part_no;
   END IF;
END;
/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top