One of our application devs came to me with an interesting question. In the following code, you'll see that the update statement is assigning the current column values to two variables, but also updating those same columns to new values.
The question was, is it guaranteed that the variables @oStart and @oFinish will be assigned to the old values of [Start] and [Finish] and not the new values? Under testing, this has been the case, however, I'd like to confirm this is the guaranteed behaviour.
I expect the answer is "no - it's not guaranteed", and ultimately, it's down to the decision the optimiser takes. Regardless, this kind of ambiguous code is to be discouraged anyway (there are more elegant and readable ways of writing it), but would be interested to hear everyone's thoughts.
-- Parameters
DECLARE @StartDate smalldatetime = '2022-01-01',
@EndDate smalldatetime = '2022-12-31';
-- Body
DECLARE @oStart smalldatetime,
@oFinish smalldatetime;
UPDATE [dbo].[MyTable]
SET @oStart = [Start],
@oFinish = [Finish],
[Start] = @StartDate,
[Finish] = @EndDate
WHERE [ID] = 12;
UPDATE tab SET a=b, b=a
, precisely because the right hand sides are effectively computed before any assignments take place.20220101
and20221231
. Additionally, for a full-year range like that, you want to define it using exclusive upper bound for the range for the day after. That said, this tends to confuse lay people who do data entry for the ranges, so it's also common to do the check for the upper range for the day after the stored value :/