I am trying to understand what happens here (one of the answers to this question: Concatenate many rows into a single text string?):
DECLARE @Names VARCHAR(8000)
SELECT @Names = COALESCE(@Names + ', ', '') + Name
FROM People
So I have an exemplary table 'test' with data:
Col1
----
abc
xyz
Now I execute:
DECLARE @Concatenation VARCHAR(8000)
SELECT @Concatenation = COALESCE(@Concatenation + ', ', '') + Col1 FROM test
Select @Concatenation
And as a result I have a single result abc, xyz
If I execute:
DECLARE @Concatenation VARCHAR(8000)
SELECT @Concatenation = Col1 FROM test
Select @Concatenation
I get a single result xyz
The question is why doing an assignment like select @variable = @variable + column causes the @variable to iterate over the whole column? And if I do it like select @variable = column which record from the column is returned? Random?