gpt4 book ai didi

sql - SQL 中的链接变量赋值是如何工作的?

转载 作者:行者123 更新时间:2023-12-02 15:35:17 24 4
gpt4 key购买 nike

我正在分析存储过程中的一些旧 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.

来源http://msdn.microsoft.com/en-us/library/ms187953.aspx

关于sql - SQL 中的链接变量赋值是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5757400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com