Say I have a bunch of rows in a DB (SQLServer 2008 in this case) that can be used to create equations.
-----------------------------------------------------
OperationID | EquationID | Operation | Amount | Order
-----------------------------------------------------
1 | 1 | + | 12 | 1
2 | 1 | + | 12 | 2
3 | 2 | / | 2 | 3
4 | 2 | + | 12 | 1
5 | 2 | - | 2 | 2
-----------------------------------------------------
I need come up with a way to evaluate the equations in this table.
Equation 1: 12 + 12 = 24
Equation 2: (12 - 2)/2 = 5
I cannot think of a way to get these results without iterating through the rows. The only ways I know how to do this is with a cursor or through the use of a temp table and a while loop. Are there any better ways to do this? If not generally what will perform better cursors or while loops?
Note: This is somewhat simplified and at this stage in the project we can only conjecture about what the data will look like. The assumption is that each 'equation' will have around 100 to 1000 operations and that there will be a few thousand 'equations' each day that will need to be processed.