>>>Hi All,
>>>
>>>Is there a way to avoid using a cursor in order to concatenate "Data" field based on the following table design:
>>>
>>>ID|Data
>>>5 This
>>>5 is
>>>5 an
>>>5 error
>>>5 message
>>>
>>>Expected query result:
>>>5 This is an error message
>>>
>>>Thank you,
>>>DANiel
>>
>>Try
>>
>>declare @d table(RowID int identity(1,1), ID int, Data varchar(100))
>>insert into @d
>>select
>>5, 'This'
>>union all select
>>5, 'is'
>>union all select
>>5, 'an'
>>union all select
>>5, 'error'
>>union all select
>>5, 'message'
>>
>>select ID, (SELECT Data as [data()]
>>FROM @d D1 where D.ID = D1.ID
>>order by RowID
>>for xml path(''),TYPE).value('.', 'varchar(max)') as Message
>>FROM @d D
>>GROUP BY ID
>>
>>See
>>Concatenate rows
>>
MSDN thread about concatenating rows>>
>>
Making a list and checking it twice>>
>>
Concatenating Rows - Part 1>>art
>>
Concatenating Rows - Part 2>
>Hang on. The answer to Daniel's question is 'No'. You cheated by adding an extra field to the table :-}
Agree :) Without a field to use in the ORDER BY, the correct order is not guaranteed.
If it's not broken, fix it until it is.
My Blog