Your understanding is not correct.
Focus on the expression:
SELECT @ColumnValue = COALESCE(@ColumnValue+',','') + ColumnValue
-------------------------------^XXXXXXXXXXXXXXX
The first argument to COALESCE()
is @ColumnValue+','
. This is NULL initially, so it gets replaced by ''
. Then comes ColumnValue
as the first element in the list.
You are likely thinking of:
SELECT @ColumnValue = COALESCE(@ColumnValue, '') + ',' + ColumnValue