A quick post today. SQL allows you to set many variables at the same time using a select statement. However, many people do not realize that when you do this the order the variables are set in the select statement actually matters.
Consider the following:
DECLARE @s VARCHAR(5),
@t VARCHAR(5);
SELECT @s = ‘test’,
@t = @s;
PRINT @t;
This will result in the expected output of @t = ‘test’
However, reversing the order.
DECLARE @s VARCHAR(5),
@t VARCHAR(5);
SELECT @t = @s,
@s = ‘test’;
PRINT @t;
We get the result @t = NULL
Has this every caused you an issue when triaging an issue?
Let us know in the comments below?