EDIT: Please disregard, I fixed it by disabling and then re-enabling the trigger. I don’t quite know why or how this fixed it as I never disabled it in the first place. Thanks all.
This trigger is meant to control the order of order_status values. So an order with a status “Dispatched” cannot become “Ready for Dispatch”, or an order with the value “Delivered” cannot become “Dispatched” etc.
It used to work fine, but today I converted the order table into an object table (well, dropped it and made a type etc) and since then it’s not worked. Constraints work fine, just not this trigger. It compiles without an issue but it simply never catches the fault. If anyone could help me here I’d appreciate it.
CREATE OR REPLACE TRIGGER Check_Order_Status
BEFORE INSERT OR UPDATE ON Customer_Order
for each row
BEGIN
IF (:new.order_status='Processed' AND :old.order_status='Delivered'
OR :new.order_status='Processed' AND :old.order_status='Dispatched'
OR :new.order_status='Dispatched' AND :old.order_status='Delivered'
OR :new.order_status='Delayed' AND :old.order_status='Delivered'
OR :new.order_status='Ready for Dispatch' AND :old.order_status='Dispatched'
OR :new.order_status='Ready for Dispatch' AND :old.order_status='Delivered'
OR :new.order_status='Dispatched' AND :old.order_status IS NULL )
then
RAISE_APPLICATION_ERROR(-20103, 'There has been an issue with the order update or insert.');
ELSE
dbms_output.put('Order Status of ' || :old.order_no || ' is now ' || :new.order_status);
END IF;
END;
.
run
Here is a minimalistic implementation of your scenario:
and your trigger compiles. So far, so good.
Let’s create a row:
Hmmm, it’s a bit silent. Let’s poke it:
Your trigger works, for me at least. So why aren’t we seeing the DBMS_OUTPUT.PUT_LINE() calls ? Don’t know, but I would guess it’s probably a bug.
Incidentally, you should extract that logic from the trigger and put it in a member function on the object. An object is a set of data and a set of related behaviours. Status validation is definitely a behaviour, so it is even more wrong to have such a trigger on an object table than it was on a normal table.