I have following quary example.

SELECT
   t1.TeamID,
   MemberList = substring((SELECT ( ', ' + FirstName )
                           FROM TeamInfo t2
                           WHERE t1.TeamID = t2.TeamID
                           ORDER BY 
                              TeamID,
                              FirstName
                           FOR XML PATH( '' )
                          ), 3, 1000 )FROM TeamInfo t1
GROUP BY TeamID

Its giving error "Incorrect syntax near the keyword 'FOR'."
Pl help.

Recommended Answers

All 6 Replies

Have you tried FOR XML AUTO ? Wondering why you don't specify a path?

If you just try:

SELECT FirstName
   FROM TeamInfo
   ORDER BY 
      TeamID,
      FirstName
   FOR XML AUTO

Do you get results?

Yes it works but with incorrect results as below.

    <TeamInfo FirstName="Bob"></TeamInfo><TeamInfo FirstName="Jim"></TeamInfo><TeamInfo FirstName="Mary"></TeamInfo><TeamInfo FirstName="Ellen"></TeamInfo><TeamInfo FirstName="Ralph"></TeamInfo><TeamInfo FirstName="Sue"></TeamInfo><TeamInfo FirstName="Bill"></TeamInfo><TeamInfo FirstName="Linda"></TeamInfo>

Expected results is 
TeamID     MemberList
1   Bob, Jim, Mary
2   Ellen, Ralph, Sue
3   Bill, Linda

What version of MSSQL are you using?

Am sure you read from the article where you found this example that it is designed for SQL 2005 and up. Earlier versions don't support the FOR XML.

commented: Thanks +0

Well, yes. But I missed.
Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.