SQLServerCentral » Programming » General » Mathematical OperationInstantForum 2016-2 FinalSQLServerCentralhttps://www.sqlservercentral.com/Forums/SQLServerCentralThu, 18 Jan 2018 22:49:23 GMT20Mathematical Operationhttps://www.sqlservercentral.com/Forums/FindPost1551533.aspxHi,
I need to write a stored proc that takes a set of input parameters and then performs mathematical operation referencing several other lookup tables and interim calculation results.
The set of input comes from a table - so I need to process all rows in the input table.
Will a while loop \ cursor be recommended for this type of operation or not at all?
The other option then will be to have several temp tables - as output from joining the lookup tables?
Please advise!
Thanks,Mon, 17 Mar 2014 17:23:45 GMTbikram.g.itRE: Mathematical Operationhttps://www.sqlservercentral.com/Forums/FindPost1551962.aspxPretty sure Tom got the high level description. The problem is - it's so generic, we have no way to provide you with advice on how to implement. Hopefully there is a way to provide SOME detail, enough to point us to that general idea, but keep enough back so you can still protect whatever idea you are trying to build.Mon, 17 Mar 2014 17:23:45 GMTMatt Miller (4)RE: Mathematical Operationhttps://www.sqlservercentral.com/Forums/FindPost1551591.aspxHi Tom,
As I mentioned there are several tables to join and one calculation feeds another table \ calculation.
So IMO (if possible) doing all that in one SQL will make it a ugly looking SQL.
Breaking it down in multiple lines is ideal from the readability \ maintenance of the calculation.
Cheers,
BikramSun, 16 Mar 2014 13:27:04 GMTbikram.g.itRE: Mathematical Operationhttps://www.sqlservercentral.com/Forums/FindPost1551589.aspxA lot depends on whether the mathematical operations needed can be performed by queries using aggregates, joins the apply operator, and so on. On what you've told us, there's nothing that says it can't be done with a single SQL statement and equally nothing that says it can be done with multiple operations and loops. As a general rule, it's a good idea to try to aoid using explicity loops (and of course explicit cursors) but there are a few cases where the general rule doesn't actually fit (usually where someone has the schema design screwed up, but some real cases too).Sun, 16 Mar 2014 12:47:21 GMTTomThomson