SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 For XML Path
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

spareus
Yak Posting Veteran

India
52 Posts

Posted - 01/07/2014 :  10:07:11  Show Profile  Reply with Quote
Getting error
Incorrect syntax near the keyword 'FOR'.
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

Pl help.


Regards,
Spareus

Lamprey
Flowing Fount of Yak Knowledge

4358 Posts

Posted - 01/07/2014 :  13:50:46  Show Profile  Reply with Quote
What version of SQL server are you using? You syntax appears to be correct to me.

Edited by - Lamprey on 01/07/2014 13:51:18
Go to Top of Page

spareus
Yak Posting Veteran

India
52 Posts

Posted - 01/08/2014 :  05:34:22  Show Profile  Reply with Quote
SQL2000.
But you are right. It works only above 2005.
Thanks for pointing out.


Regards,
Spareus
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 01/08/2014 :  07:02:30  Show Profile  Reply with Quote
for sql 2000 you need to use UDF method


CREATE FUNCTION ConcatValues
(
@TeamID int
)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @RetVar varchar(8000)

SELECT @RetVar=COALESCE(@RetVar,'') +  ', ' + FirstName 
FROM TeamInfo 
WHERE TeamID = @TeamID
ORDER BY TeamID,FirstName

SET @RetVar= substring(@RetVar,3,8000)
RETURN (@RetVar)
END

then call it like below

SELECT
   t1.TeamID,
   MemberList = dbo.ConcatValues(t1.TeamID)
GROUP BY TeamID


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000