Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How stop insert action in trigger

I am using Oracle Apex. I need to create a trigger for checking the no. of stock whether < 0. If the no. of stock < 0 then insert action is stopped then prompt a alert message to user. Below coding dose not work. what is wrong of my code? please help.

My coding:

CREATE OR REPLACE TRIGGER cw_service_b4_trigger
BEFORE INSERT ON cw_serviceline
FOR EACH ROW

DECLARE

 exist_stock number;

BEGIN
    select stock into exist_stock from cw_inventory where inv_id = :new.inv_id;
       if (exist_stock - :new.quantity) < 0 then
         dbms_output.put_line ('Out of Stock');
         return;
       end if;
END;
/
like image 834
Patrick Lee Avatar asked Oct 18 '25 11:10

Patrick Lee


1 Answers

You haven't thrown any sort of error. All your trigger does is output a message.

return doesn't cause the INSERT to stop, it just causes the trigger logic to return.

Instead you need to raise an error to cause the insert to fail.

if exist_stock < :new.quantity then
    raise_application_error(-20000, 'Out of stock'); 
end if;
like image 133
codenheim Avatar answered Oct 20 '25 09:10

codenheim