SQL 2005/8 古怪的更新“黑魔法”
大家好,所有 SQL Server Blackbelt 大师。
我有一个简单的问题需要解决。我正在尝试对桌子进行一些奇怪的更新。表的具体结构并不重要,我的问题本质上可以简化为以下问题:
update SalesTotal
set @total = total = @total + sales,
@flag = flag = case when @flag = 1 then 0
when @total > x then 1
else 0 end
问题是我是否可以相信@total变量将具有新值,即。将在第二个设置部分之前执行,或者它包含一个“旧”值(来自先前的赋值)或者它是未定义的。我已经解决这个问题有一段时间了,找不到解决方案,这对我的更新非常重要(我想一次性更新一张表)。我想要更新的一个值取决于另一个值(在同一行中),因此它在单个更新中工作的唯一方法是它是否有效。
希望你能帮助我
Hi all SQL Server Blackbelt masters out there.
I have a simple question that I need to solve. I am trying to do some quirky update on a table. Specific structure of the table is not important and my problem can essentially be reduced to the following one:
update SalesTotal
set @total = total = @total + sales,
@flag = flag = case when @flag = 1 then 0
when @total > x then 1
else 0 end
The question is CAN I TRUST that @total variable will have a new value ie. will execute before the second set part, or will it contain an "old" value (from previous assignment) or it is undefined. I am pursuing this problem for some time now, and can't find a solution and it is really important for my update (I want to update a table in one go). One of the values I want to update depends on the value of the other (in the same row) so the only way for it to work in a single update is if it works.
Hope You can help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,您不能对命令式执行顺序做出任何假设。绝对不能保证表中更新位置的顺序,绝对不能保证@variable 为每个表集更新一次,并且更新可能会通过 万圣节保护。
No, you cannot make any assumption of imperative order of execution. There is absolutely no guarantee about the order of the updates tacking place in the table, there is absolutely no guarantee that the @variable is updated once for each table SET, and the update may well run with a spool step for Halloween protection.