I have a list of products in a table, I wanted to create a trigger that reads the stock in hand for a particular product and then if the stock is less than 5 display a message. the concept is pretty simple.
Here is what i have so far.
SET SERVEROUTPUT ON
SET ECHO ON
CREATE OR REPLACE TRIGGER TRG_REORDERSTOCK
AFTER INSERT OR UPDATE OF S_QUANTITY ON STOCK_INVENTORY
FOR EACH ROW
BEGIN
IF :OLD.S_QUANTITY <= 10 THEN
DBMS_OUTPUT.PUT_LINE ('Warning: ----- product with ID (' || :OLD.BR_ID || ') has (' || :NEW.S_QUANTITY || ') units remaining, please re-order -----' );
ELSE
DBMS_OUTPUT.PUT_LINE ('UPDATE COMPLETE');
END IF;
END;
/
Now to test the trigger i am going to update the quantity in hand for a particular product;
UPDATE STOCK_INVENTORY
SET S_QUANTITY = 4
WHERE BR_ID = 1
AND P_ID = 6;
And the result is :
Warning: ----- product with ID (1) has (4) units remaining, please re-order -----
which means that the trigger works. However the stock_inventory has a few products that stock level is less than 10, but with the conditional I have, it only shows the message for the current transaction.
here is a list of a few products in stock_inventory, this is a linker table;
SQL> SELECT * FROM STOCK_INVENTORY;
BR_ID| P_ID|S_QUANTITY
----------|----------|----------
1| 1| 10
1| 6| 4
1| 3| 30
1| 8| 24
1| 9| 18
2| 10| 9
2| 2| 10
2| 20| 15
2| 16| 17
2| 13| 20
3| 21| 15
How can I show a list of the products where quantity is less than 10? I am pretty new to oracle. Thanks alot.
Thanks