Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

SQL Server 2008 - Concatena stringa

Se vuoi concatenare i valori tra le righe, usa il trucco FOR XML, ad esempio:

SELECT Name + ',' 
FROM Project
FOR XML PATH('') 

Ecco un esempio più completo:

select LineItemID, (
        Select m.Material + ','
        From test.Materials m 
        inner join test.LineItems_Materials lm1 on m.MaterialID = lm1.MaterialID 
        Where m.MaterialID in (select MaterialID from test.LineItems_Materials where LineItemID = lm2.LineItemID)
        FOR XML PATH('')  
    ) as Materials
from test.LineItems_Materials lm2
group by LineItemID