|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Trigger after insert and update on the same table?
Hi all,
may be this going to be an easy answer for those who know. Unfortunatelly not for me as I am beginner with Firebird. I have got the following problem. Let's say I have table of items for orders and because I need to avoid of rounding errors I decide to use trigger to calculate total price for each item (price*quantity*tax) and store this price for such item in the same table's row (there is extra column dedicated to this). This price will be use later then. If I use after insert trigger, then it's OK. But someone might change the order and therefore I need also after update. So after INSERT statement is done then I can do UPDATE table SET... (to set total prices) but this will cause firing of after update trigger and still so on. I wonder if someone can explain me what is the common solution for such situation (without using extra table, which is easy)? The database is Firebird 1.5. Thank you for any suggestions. |
|
#2
|
||||
|
||||
|
I'm sorry, but the common solution is a view which calculates total price on the fly and with the rounding rule you specified in building it.
__________________
My blog Tutorials about OSS databases, DBMonster ... Contribute to OSS Development, fill bug reports! Developer Shed eSupport Commented my.ini/my.cnf (ADD YOUR OWN CONFIG TRICK) An introduction to database normalization Natural or Surrogate key Custom ordering for your results Correlated and uncorrelated subqueries Don't turn your outer joins into inner joins Random data (with a bias) |
|
#3
|
|||
|
|||
|
Well, I have to use views then...unless there are some good ideas out there?
I have one more question. If I specify trigger as BEFORE INSERT and then use the syntax in it like NEW.TOTAL_PRICE=NEW.PRICE*NEW.TAX Would this be possible or I have to use always "UPDATE table SET ..." if the trigger do something with the same table to which it is connected to? |
|
#4
|
|||
|
|||
|
Good practice to keep actual price in item line
Jiri,
Use a before insert trigger to set initial value and a before update trigger to make sure changes propogate. like New.LineTotal = RoundFunction(New.ItemCount * New.ItemPrice * New.TaxRate) where RoundFunction is your rounding function. These will not trigger more events |
![]() |
| Viewing: Dev Shed Forums > Databases > Firebird SQL Development > Trigger after insert and update on the same table? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|