>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 rowsMaking a list and checking it twiceConcatenating Rows - Part 1Concatenating Rows - Part 2
If it's not broken, fix it until it is.
My Blog