Triggers and Exception Handling
737995Dec 20 2009 — edited Dec 20 2009I am just learning how to use triggers so if this is a dumb question, sorry about that!
I created 2 tables, one called PRODUCT_PRICES(PRO_ID NUMBER, PRICE NUMBER) and the other called PRICE_RANGE(PRO_ID NUMBER, MAX_PRICE NUMBER, MIN_PRICE NUMBER).
What I'm supposed to do is create a before insert or update trigger on the table product_prices that issues 2 errors:
1.“No such product” when PRO_ID is not in PRICE_RANGE table
2.“The price ... is not within the range for product ..”
This is what I have so far:
CREATE OR REPLACE TRIGGER PRODUCTS_PRICES_TRG
BEFORE
INSERT OR UPDATE
ON PRODUCT_PRICES
FOR EACH ROW
DECLARE
INVALID_PRODUCT EXCEPTION;
INVALID_PRICE EXCEPTION;
BEGIN
IF (:NEW.PRO_ID IN PRICE_RANGE) THEN
RAISE INVALID_PRODUCT;
ELSE IF (:PRICE IN PRICE_RANGE >= MAX_PRICE OR <= MIN_PRICE) THEN
RAISE INVALID_PRICE;
END IF;
EXCEPTION
WHEN INVALID_PRODUCT THEN
BEGIN
RAISE_APPLICATION_ERROR(-20001, ‘NO SUCH PRODUCT’);
END;
WHEN INVALID_PRICE THEN
BEGIN
RAISE_APPLICATION_ERROR(-20002, ‘THE PRICE IS NOT IN THE RANGE FOR PRODUCT');
END;
I believe what I am confused about is I'm not really sure how the begin section with the if statements works, and I'm not sure how to use 2 tables with one trigger like this. Any suggestions would be great.