Suppose I have a situation where I have to solve a multi-variable equation that could be broken down in the following way.
Var1 = <some equation>
Var2 = <some equation involving Var1>
Var3 = <some equation involving Var1 and / or Var 2>
FinalVal = <some equation using any of the other variables>
In my SQL Server 2012 environment, I found that if I set up a statement to set the variables in the following way:
DECLARE
@Var1
,@Var2
,@Var3
,@FinalVal
;
SELECT
@Var1 = ...
,@Var2 = ...@Var1...
,@Var3 = ...@Var1, @Var2...
,@FinalVal = ...@Var1, @Var2, @Var3...
FROM
MyTable
I could do it and get the correct values - Meaning @Var1
seems to be set first and the order of setting the variables continues down the line in the order of the select statement.
Is this something we can be sure to be the case - Meaning is it safe to set variables in this way? It has the advantage of only having to query the table once and all the logic is placed in a single statement rather than multiple SET
statements. Or is this not a safe practice / should be avoided?
I know this may require some opinion on safety, but I'm moreso hoping there is a definitive answer of "yes, this is the way SQL Server will do it" or not that I can know the best way to move forward.
Thanks!