database - How to use if statement in a trigger using SQL -


i have following tables in ibmdb2 database:

produt(pid....etc) combosandpromotions(cp_id, cp_price...etc) pricesize(size, price) sales(pid, cp_id, size, quantity, sales_price)

i want create trigger automatically calculate value of sales_price. basically

1-if pid null value price of cp_id * quantity.

2- if cp_id null value of sales_price price of size of product bought(the price depends on size not on pid) * quantity.

3-if both of them not null value of sales_price equal sum of both of previous summations. have tried following sql code, it's not working.

create trigger calc_price after insert on sales every row mode db2sql   if cp_id null update table sales set price = (pricesize.price)*quantity sales.size = pricesize.size  if pid null update table sales set price = combosandpromotions.cp_price * quantity combosandpromotions.cp_id = sales.cp_id 

can assist me on how correct since don't have experience sql. thank you.

it's been long time since used db/2, might work:

create trigger calc_price after insert on sales referencing new n every row mode db2sql  begin atomic     declare cpprice int;      if n.cp_id null         update table sales             set price = (n.price) * n.quantity         pid = n.pid     end if;      if n.pid null         set cpprice = (select cp_price combosandpromotions cp_id = n.cp_id)         update table sales            set price = cpprice * n.quantity         pid = n.pid     end if; end 

the idea reference newly inserted row (here n) , use key row update table. (this might work better before insert..?)


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -