Firebird SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesFirebird SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 9th, 2004, 07:28 AM
Jiri Jiri is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 6 Jiri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 46 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old July 9th, 2004, 08:07 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,917 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 3 Days 14 h 31 m 53 sec
Reputation Power: 279
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.

Reply With Quote
  #3  
Old July 9th, 2004, 08:17 AM
Jiri Jiri is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 6 Jiri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 46 sec
Reputation Power: 0
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?

Reply With Quote
  #4  
Old August 20th, 2004, 09:56 PM
mikca mikca is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 7 mikca User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > Trigger after insert and update on the same table?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT