Question

so i am trying to write a trigger that takes an incoming order and checks to make sure there is enough inventory to cover the order. if there is not then don't insert the order. i am getting the correct test output, but i don't know how to stop the insert.

the output I'm getting is: 1 rows inserted. ERROR: QUANTITY 144444 EXCEEDS INVENTORY ONHAND [11]

which makes me wonder if the trigger is working as desired.

        create or replace TRIGGER INVENTORY_AVAIL 
    Before INSERT
       ON ORDER
       FOR EACH ROW

    DECLARE
       v_quantity_diff number;
       v_onhand_quantity number;

    BEGIN
       -- TRIGGR ON THIS...
       --INSERT INTO THC_ORDER
       --( FK_ORDER_NO, FK_PROD_ID, QUANITY , COMPLETE_STATUS)
       --VALUES
       --( :NEW.FK_ORDER_NO, :NEW.FK_PROD_ID, :NEW.QUANITY , :NEW.COMPLETE_STATUS);
       SELECT INVENTORY_ONHAND INTO v_onhand_quantity
       FROM INVENTORY 
       WHERE :NEW.fk_prod_id = INVENTORY.FK_PROD_ID;

      IF( (v_onhand_quantity - :NEW.QUANTITY) >= 0) 
        THEN
         DBMS_OUTPUT.PUT_LINE('GOOD: QUANTITY ORDERED ' || :NEW.QUANTITY 
             || ' ... LEAVES [' || TO_CHAR((v_onhand_quantity - :NEW.QUANTITY)) 
             || ']  INVENTORY ONHAND ' ); 
        DBMS_OUTPUT.PUT_LINE('SUCCESS');

      ELSE
       DBMS_OUTPUT.PUT_LINE('ERROR: QUANTITY ' || :NEW.QUANTITY 
           || ' EXCEEDS INVENTORY ONHAND [' || TO_CHAR(v_onhand_quantity) || ']' ); 
      END IF;

    END;
Was it helpful?

Solution

You'll have to raise an exception. In your case, replace

   DBMS_OUTPUT.PUT_LINE('ERROR: QUANTITY ' || :NEW.QUANTITY 
       || ' EXCEEDS INVENTORY ONHAND [' || TO_CHAR(v_onhand_quantity) || ']' ); 

by

raise_application_error (-20001,''ERROR: QUANTITY ' || :NEW.QUANTITY 
       || ' EXCEEDS INVENTORY ONHAND [' || TO_CHAR(v_onhand_quantity) || ']' ); 

OTHER TIPS

You use trigger BEFORE INSERT, which fires up your code, and then inserts the stuff to a table. Try using INSTEAD OF trigger, which will handle also INSERT statements if desired critria are met.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top