1

I have the following tables in my IBMDB2 database:

Produt(PID....etc) CombosAndPromotions(CP_ID, CP_Price...etc) PriceSize(size, price) Sales(PID, CP_ID, size, quantity, Sales_Price)

I want to create a trigger to automatically calculate the value of the Sales_Price. Basically

1-if the PID is null then the value is the price of the CP_ID * quantity.

2- if the CP_ID is null then the value of the Sales_Price is the price of the size of the Product bought(the price depends on the size not on the PID) * the quantity.

3-if Both of them are not null then the value of the Sales_Price is equal to the sum of both of the previous summations. I have tried the following SQL code, but it's not working.

create trigger calc_Price
after insert on sales
for every row mode db2sql 

if CP_ID is null
update table Sales
set Price = (PriceSize.price)*Quantity
where Sales.size = PriceSize.size

if PID is null
update table Sales
set price = CombosAndPromotions.CP_price * quantity
where CombosAndPromotions.CP_ID = Sales.CP_ID

can someone assist me on how to correct it since I don't have experience with sql. thank you.

4
  • Triggers are very database specific.. which database are you using? Commented Aug 18, 2014 at 16:00
  • @thebjorn i'm using IBMdb2 Commented Aug 18, 2014 at 16:03
  • Then you need the then keyword and end if; in your if statements (certain other databases like sql server and sybase don't require this). You also need a begin and end around the trigger body (possibly with atomic after begin..) Commented Aug 18, 2014 at 16:05
  • @thebjorn can you please give me a simple demonstration on how can i use these keys, i will really appreciate your help Commented Aug 18, 2014 at 16:08

1 Answer 1

1

It's been a very long time since I used db/2, but something like this might work:

create trigger calc_Price
after insert on sales
referencing new as n
for every row mode db2sql 
begin atomic
    declare cpprice int;

    if n.CP_ID is null then
        update table Sales
            set Price = (n.price) * n.Quantity
        where PID = n.PID
    end if;

    if n.PID is null then
        set cpprice = (select CP_price from CombosAndPromotions where CP_ID = n.CP_ID)
        update table Sales
           set price = cpprice * n.quantity
        where PID = n.PID
    end if;
end

The idea is to reference the newly inserted row (here as n) and use the key from that row to update the table. (this might work better as a before insert..?)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot @thebjorn, although the code is not working but it will help me fix the errors and correct everything. thank you
Please feel free to edit my answer with the correct code once you get it working :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.