Tuesday, 10 September 2013

Oracle Trigger with condition

Oracle Trigger with condition

I have this trigger which getting near to what I want to accomplished but
after trying I still cannot make it done.
What I've done
CREATE or REPLACE TRIGGER TR_UPD_TEST_TABLE_B
AFTER INSERT ON TEST_TABLE_A
FOR EACH ROW
DECLARE
BEGIN
FOR VAL IN (SELECT * FROM TEST_TABLE_B A WHERE A.ID = :NEW.ID) LOOP
IF :NEW.QTY_RECEIVED > VAL.QTY_PRE_RECEIVED THEN
UPDATE TEST_TABLE_B A SET A.QTY_RECEIVED = 888 WHERE A.ID = :NEW.ID;
ELSE
UPDATE TEST_TABLE_B A SET A.QTY_RECEIVED = 111 WHERE A.ID = :NEW.ID;
END IF;
END LOOP;
END;
The Problem
By using IF :NEW.QTY_RECEIVED > VAL.QTY_PRE_RECEIVED, I want to compare
the :NEW.QTY_RECEIVED with each record but it seems that it total up all
the records' quantity. Therefore the statement always jump to the ELSE
because the total quantity is bigger than the :NEW.QTY_RECEIVED.

No comments:

Post a Comment