Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a table col1 float, col2 float with the following data:

1,2

3,4

After the query:

UPDATE MyTable SET col1 = 100, col2 = col1

is executed, the rows are:

100,1

100,3

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.

Regards,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
221 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here's another alternative to try:

DECLARE @x float;

UPDATE MyTable
SET
  @x = col1 = formula,
  col2 = @x * …
OPTION (MAXDOP 1)

or:

DECLARE @x float;

UPDATE MyTable
SET
  @x = formula,
  col1 = @x,
  col2 = @x * …
OPTION (MAXDOP 1)

OPTION (MAXDOP 1) is there to ensure the sequential order of evaluation of assignments.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...