Re: Aggregate functions

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Steve Kass (skass_at_drew.edu)
Date: 06/28/04


Date: Sun, 27 Jun 2004 21:18:16 -0400

Nigel,

  The use of variables is documented, but as far as I know, if a select
statement doesn't produce a result set, the only guarantee here is that
@w will be assigned at least one value, and no guarantee that there will
be one assignment for every row in the table, let alone any guarantee
about the order of assignments. The only mention of this I know of is
http://support.microsoft.com/default.aspx?scid=kb;en-us;287515, which
says "The correct behavior for an aggregate concatenation query is
undefined", and while it also says "In order to achieve the expected
results from an aggregate concatenation query, apply any Transact-SQL
function or expression to the columns in the SELECT list rather than in
the ORDER BY clause," I have my doubts whether that sole off-handed
remark in a relatively confusing KB article is a real indication that
Microsoft intends this technique to be reliable.

SK

Nigel Rivett wrote:

>Think it's documented in a white paper somewhere - I've lost the reference now.
>
>Should be
>declare @w varchar(100)
> select @w=coalesce(@w+',','') +t from w where id=@id
> return @w
>
>
>
>"Steve Kass" wrote:
>
>
>
>>Uri,
>>
>> While this works in many situations, it's not supported or documented,
>>and I don't recommend using it in a production environment.
>>
>>SK
>>
>>Uri Dimant wrote:
>>
>>
>>
>>>impafait
>>>You dont have to know a maximum number of items
>>>Look at below soultion works for you
>>>create table w
>>>(
>>>id int,
>>>t varchar(50)
>>>)
>>>
>>>insert into w values (1,'abc')
>>>insert into w values (1,'def')
>>>insert into w values (1,'ghi')
>>>insert into w values (2,'ABC')
>>>insert into w values (2,'DEF')
>>>select * from w
>>>
>>>
>>>create function dbo.fn_my ( @id int)
>>>returns varchar(100)
>>>as
>>>begin
>>>declare @w varchar(100)
>>> set @w=''
>>> select @w=@w+t+',' from w where id=@id
>>> return @w
>>>end
>>>
>>>select id,
>>>dbo.fn_my (dd.id)
>>>from
>>>(
>>>select distinct id from w
>>>)
>>>as dd
>>>
>>>drop function dbo.fn_my
>>>"imparfait" <imparfait@noway.noway> wrote in message
>>>news:%23ZsOuM6WEHA.3640@TK2MSFTNGP11.phx.gbl...
>>>
>>>
>>>
>>>
>>>>Thank you very much for ur precise and detailed answer.
>>>>
>>>>
>>>>"Steve Kass" <skass@drew.edu> a écrit dans le message de
>>>>news:eH6Yxx5WEHA.2520@TK2MSFTNGP12.phx.gbl...
>>>>
>>>>
>>>>
>>>>
>>>>>There is no feature of SQL Server specifically for this requirement,
>>>>>though user-defined aggregates may be available in SQL Server 2005.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>>
>>
>>



Relevant Pages

  • Re: WaitForSingleObject() will not deadlock
    ... Even though the assignment of value 2 is guaranteed ... compiler's guarantee), without the specific guarantee from the thread ... the *compiler* does not guarantee this. ... Any data written to memory after the new thread is ...
    (microsoft.public.vc.mfc)
  • Re: Abstract and concrete classes in c++
    ... Well, if your base class has a copy assignment that can throw, then you ... The fact is that I myself do not require the strong guarantee for my own ... Dave Moore ...
    (comp.lang.cpp)
  • Re: Template-based implementation of Sutters exception-safe operator= idiom
    ... For those who do need the strong guarantee at a higher level, ... Take your example where a client wants an assignment followed by ... template <typename C, typename T> ... C temp(src); ...
    (comp.lang.cpp)
  • Re: Constructor
    ... MyClass object = new MyClass; ... Do I have a guarantee that object is not null when I call doStuff? ... For that read the JLS on "definite assignment". ... Foo foo; // I hate including 'Class' or 'object' in identifiers ...
    (comp.lang.java.programmer)
  • Re: Constructor
    ... MyClass object = new MyClass; ... Do I have a guarantee that object is not null when I call doStuff? ... For that read the JLS on "definite assignment". ... Foo foo; // I hate including 'Class' or 'object' in identifiers ...
    (comp.lang.java.programmer)