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.
thenkeyword andend if;in your if statements (certain other databases like sql server and sybase don't require this). You also need abeginandendaround the trigger body (possibly withatomicafterbegin..)