One of the small quirks of SQL is that it can add each row to a variable.
In the most common case, this is used to generate a comma delimited list.
The script shows how to do this wiht a scalar function, so that multiple rows are returned.
The script shows calculating the column sum the hard way and compares the result to the standard aggregate function SUM().
Of interest is that apart from the output, both sum and incrementing the variable had the same execution plan cost.
2007-10-02 (first published: 2002-06-20)
15,451 reads