Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Query help
Message
 
 
À
18/11/2010 17:12:08
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
Versions des environnements
SQL Server:
SQL Server 2005
Divers
Thread ID:
01489661
Message ID:
01489684
Vues:
36
>>>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
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform