• I got it right because I knew it, but I understand why that might not be obvious, and the behaviour is slightly iffy.

    If you do

    SELECT @var = MAX(object_id)

    FROM sys.objects

    WHERE 1=0

    you'll get NULL assigned, but

    SELECT @var = object_id

    FROM sys.objects

    WHERE 1=0

    will retain the previous value of @var.

    It's really a source for unexpected behaviour when a WHERE-clause is a little wrong, and relying on it when coding isn't really fair to whoever will maintain your code.


    Just because you're right doesn't mean everybody else is wrong.