Home Improve performance of SQL query with a FOR XML PATH in SQL Server 2008
Reply: 0

Improve performance of SQL query with a FOR XML PATH in SQL Server 2008

user596
1#
user596 Published in May 25, 2018, 2:21 pm

I am trying to concatenate LoadIds for a user using For XML path which is a portion of my whole query, I verified that the maximum time gets elapsed in calculating this concatenated LoadId column. Below is the syntax, can anyone suggest a way to rewrite this efficiently?

SELECT 
    Col1, col2, 
    LoadIds = STUFF((SELECT ' , ' + CAST([LoadId] AS varchar(5))
                     FROM Table1 AS t1
                     WHERE t1.[UserId] = [t2].[UserId]
                     FOR XML PATH ('')), 1, 2, '')
FROM 
    Table1 AS t2
GROUP BY 
    [UserId]
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.430336 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO