Way to reference a column dynamically within a trigger

oraclestored-procedurestrigger

I asked a similar question a few months back and I'm improving upon my solution!

I have an Oracle database where I would like to trigger a procedure to do additional work on a column upon updating of another.

For example, say that if I mark an item on sale, I would like the database to automatically take 20% off the price. So:

| ID | ITEM                     | PRICE | ONSALE | DISCOUNT1 | DISCOUNT2 |
|----|--------------------------|-------|--------|-----------|-----------|
| 1  | T-Shirt                  | $20   | F      | .15       | .3        |
| 2  | Sweatshirt               | $30   | F      | .25       | .5        |
| 3  | Jeans                    | $40   | F      | .1        | .4        |
| 4  | Fashionably Ripped Jeans | $120  | F      | .05       | .1        |

What if I wanted the trigger to, say, programmatically select DISCOUNT1 or DISCOUNT2, how would I do this?

:NEW.PRICE := :NEW.PRICE - 
    (:NEW.PRICE * :NEW.DISCOUNT(do tricky math to get a random number));

Best Answer

I was not able to solve this issue in the method I had wanted. I used an if statement as suggested by Serg.

if ((:NEW.ONSALE = 'T') AND (:OLD.ONSALE = 'F')) then
    :NEW.PRICE := :OLD.PRICE - :OLD.PRICE * :NEW.DISCOUNT1;
elsif ((:NEW.ONSALE = 'T') and (:OLD.ONSALE = 'T')) then
    :NEW.PRICE := :OLD.PRICE - :OLD.PRICE * :NEW.DISCOUNT2;
end if;

This was a very simplified example but I couldn't figure out a better way to do this.