title: Proper Way to do Calculations in SQL
body: <pre><code>Baseline Table
  
  ProjectId   ProjectName    Forecast
  ---------   -----------    --------
    11258      Test Proj1      0.678 
    11259      Test Proj2      2.57 
  
  FundEntity Baseline Table
  
  ProjectId    FundEntityId    Forecast    ForecastDollars
  ---------    -----------     --------    ---------------
    11258          5             0.226
    11258          8             0.226
    11258          11            0.226 
  </code></pre>
  
  <p>I have a stored procedure that loops through each entry in the ProjectSummaryEntity Table (Yuck!) and for each one, break the forecast down based on "allocations" set in the FundEntity table. Then, I need to calculate the dollar amount of the forecast based on a certain rate in a "Rate" table. The rate is determined by the FundEntity as well.</p>
  
  <p>The stored procedure is running a bit slow (14s for 6.6k rows) and I'm not surprised due to inefficiency of code. My question is, how do I do "on the fly" calculations without using cursors? </p>
  
  <p>The reason why I use cursors is that:</p>
  
  <ul>
  <li>forecast amount is broken down first (from ProjectSummary), saved to a variable</li>
  <li>rate is determined, saved to variable</li>
  <li>dollar amount is determined, saved to a variable</li>
  <li>final update statement is run to update forecast and forecast dollars</li>
  </ul>
  
  <p>If I don't use cursors there's no way to update the dollars based on the forecast. Any help is appreciated. Thanks!</p>
  
  <p>EDIT: The above is a really simplified version of the whole thing. The stored procedure that deals with this is in: <a href="http://pastebin.ubuntu.com/629888/" rel="nofollow">http://pastebin.ubuntu.com/629888/</a> </p>