I have a table that I need to move a value from one column to another then set a new value in the original column.
declare @foo table (A int, B int);
insert into @Foo select 1, 0;
update @Foo
set B = A,
A = 2;
After the update does B always contain 1 or is this non deterministic behavior and it will sometimes have a value of 2 due to A being updated first (and all my tests have just never hit just right the conditions to have it be 2)?
As a followup question if the answer is “B will always be 1“, if I do the following will I still get the same result?
update @Foo
set A = 2,
B = A;
When doing an
update, SQL Server keeps two versions of the row. The old one (akadeleted) and the new one (akainserted.) The assignment itself only modifies theinsertedrow. All assignments use the same variable values from thedeletedrow. The order of the assignment does not matter.You can visualize this using the
outputclause: