我正在分析存储过程中的一些旧sql代码.
Declare @Var1 money,@Var2 money,@Var3 money,
等等…
Select @Var1 = OldValue,@Var2 = @Var1,
等等…
所以我想知道这些分配在同一个select语句中是如何工作的.在调用select之后,我假设Var2 = OldValue,但我想确定.
这种情况有哪些规则?分配是按照声明的顺序执行的吗?如果是这样,在以下情况下将为Var2分配什么值:
Select @Var2 = @Var1,@Var1 = OldValue,
谢谢!
解决方法
DECLARE @Var1 MONEY = 100,@Var2 MONEY = 50 SELECT @Var1 = @Var2,@Var2 = @Var1 SELECT @Var1,@Var2
返回
--------------------- --------------------- 50.00 50.00
所以在那种情况下,他们是按照从左到右的顺序执行的,但这不能依赖!
If there are multiple assignment
clauses in a single SELECT statement,
sql Server does not guarantee the
order of evaluation of the
expressions. Note that effects are
only visible if there are references
among the assignments.