Assign a value into variable from empty set cause the value remains the same.
Assign a value into variable from empty set which include an aggregate cause the value will be set to the value returned from aggregate. One row is returned everytime, even if no rows were aggregated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | declare @x int declare @table table( a int not null ) insert into @table values(10) --1) set @x =-1 select @x = min(a) from @table where 1=2 select @x --2) set @x =-1 select @x = a from @table where 1=2 select @x /* ----------- NULL ----------- -1 */ |