• I like the idea of the question - testing when an assignment to a variable with CASE would or would not overwrite previous assignments. I can see some people getting that wrong.

    Unfortunately, the execution of the idea left to be desired. The code was long and complicated, making this question more an exercise in parsing code. The exact same question idea could have been done with a much shorter code sample:

    DECLARE @Str1 varchar(10) = '',

    @Str2 varchar(10) = '',

    @val int;

    SET @val = 1;

    SELECT @Str1 = CASE WHEN @val = 1 THEN 'One' ELSE NULL END,

    @Str2 = CASE WHEN @val = 2 THEN 'Two' ELSE NULL END;

    SET @val = 2;

    SELECT @Str1 = CASE WHEN @val = 1 THEN 'One' ELSE NULL END,

    @Str2 = CASE WHEN @val = 2 THEN 'Two' ELSE NULL END;

    SELECT @Str1, @Str2;

    Sasidhar Pulivarthi, you obviously have good ideas for questions. I hope you will continue to submit question, and that you will take this feedback to try and convert your good ideas into even better questions going forward.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/