SQL Server:更新 MyTable SET col1 = 值,col2 = col1 [英] SQL Server : UPDATE MyTable SET col1 = value, col2 = col1
问题描述
我有一个表 col1 float, col2 float ,其中包含以下数据:
I have a table col1 float, col2 float with the following data:
1,2
3,4
查询后:
UPDATE MyTable SET col1 = 100, col2 = col1
被执行,行是:
100,1
100,3
显然,当 col2 正在更新时,会使用 col1 的旧的、未更新的值.是否可以使用新值更新 col2,引用 col1 或者我必须将值 (100) 写入两次?我想使用自动生成的查询,并且更容易保留公式.
Obviously, the old, not updated value of col1 is used, when col2 is being updated. Is it possible to update the col2 with the new value, referring col1 or I have to write the value (100) twice? I want to use auto-generated queries and it's easier to keep formulas.
问候,
推荐答案
这是另一种尝试的替代方案:
Here's another alternative to try:
DECLARE @x float;
UPDATE MyTable
SET
@x = col1 = formula,
col2 = @x * …
OPTION (MAXDOP 1)
或:
DECLARE @x float;
UPDATE MyTable
SET
@x = formula,
col1 = @x,
col2 = @x * …
OPTION (MAXDOP 1)
OPTION (MAXDOP 1)
用于确保分配评估的顺序.
OPTION (MAXDOP 1)
is there to ensure the sequential order of evaluation of assignments.
这篇关于SQL Server:更新 MyTable SET col1 = 值,col2 = col1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!