[quoted text, click to view] David Scemama wrote:
> Bob Barrows [MVP] wrote:
>> David Scemama wrote:
>>
>>> Hi,
>>>
>>> I would like to write a stored procedure that select all the values
>>> of one column and concatenate them to produce an output string.
>>>
>>> something like
>>>
>>> select %names = ConcatAllTheValues(name+";") from myTable where
>>> age=3 I'm sorry if the question may seem stupid, but I'm not an expert
>>> in
>>> SP.
>>
>> Well, it's not really supposed to work, and there's no guarantee it
>> will keep working in future versions of SQL Server, b ut it is
>> possible to do this:
>>
>> declare @str varchar(8000)
>> select @str = @str + colname from ...
>>
>> You would be better advised to do this in the client.
>>
>> Bob Barrows
>>
>
> I really understand that doing the concatenation on the client allows
> more control on the operation. But in terms of performance, isn't
> there a real benefit in doing it on the server side ?
>
Test both methods and see for yourself. We really cannot answer in a vacuum.
My gut says that doing a set-based operation on the server would be more
efficient than a looping operation in the client, but:
1. I'm not really sure this behavior (it's been referred to as aggregate
concatenation, but MS will not give it an official name) qualifies as a
set-based operation - the engine may resort to a cursor loop behind the
scenes to accomplish it.
2. You may be using a client that is really efficient at looping
3. Looping can be avoided through the use of the ADO recordset GetString
method, which can build a string very efficiently.
4. It really is not supported, and you would be taking a risk using it.
Having said that, I used it in a couple of early projects (SQL 6.5), before
I "knew better", and have never had a problem. The projects have been ported
first to SQL7, then to SQL 2000 without any rewrites. Just don't try to
control the order of the items that end up in the list - that is where
problems can potentially occur.
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"