I am trying to use a trigger to avoid a deletion of a row in table Products
under some circumstances. The code is the following
CREATE TRIGGER trgPreventProductRemoval
BEFORE DELETE ON Products
FOR EACH ROW
BEGIN
DECLARE
l_custid INTEGER;
BEGIN
SELECT count(*) INTO l_custid FROM Orders WHERE product = :old.prodDescription ;
IF l_custid > 0 THEN
raise_application_error (-20100, 'You can not delete a product that has active orders!');
END IF;
END;
END;
However I get the error: table ORDERS is mutating, trigger/function may not see it
.
How can I fix it?
EDIT-SOLUTION: I have accepted the solution below because it is the "politically correct". Due to some "restrictions" I was unable to use it and finally found a different workaround. See the solution that I have posted seperately.