Solved

sql server 2000  Incorrect syntax near 'xml'.

Posted on 2008-10-28
10
1,233 Views
Last Modified: 2012-08-13
this query doesnot seem , to work properly in db ....
my table structure
table1:foodtable field                                            
fid int(identity(1,1),primary)
fname varchar                                                        
table 2:Resttable          
1.Rid primary key                                                  
2. resname varchar                                                      
table3 : MAintable                                                    
1.pid int(identity(1,1),primary)                                    
2.fid int foriegn key(foodtable)
3..rid  int  foreign key(resttable)
4.price float    

select  distinct fid,(select  distinct fname from foodtable where fid=maintable.fid)as fname,stuff( (select ','+resname from resttable where rid = maintable.rid for xml path('')),1,1,'') as resname  from maintable

i get this error,

Incorrect syntax near 'xml'.

Plzz suggest a fine query ...


0
Comment
Question by:Rajar Ahmed
  • 6
  • 3
10 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22821926
that syntax only works in sql 2005 or higher.
in sql 2000, you will need a UDF function solution instead.
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22822129
ho yaaa...exactly correct as u said , the server was 2005 version thanks for cleaing the doubt ...
but can say me a query function which works in sql server2000 .
since i mostly use db from localdb which is sql server 2000.
 
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22822163
you will need to create this function:
CREATE FUNCTION dbo.ConcatFoodName(@rid int)

RETURNS VARCHAR(8000)

AS

BEGIN

 DECLARE @res VARCHAR(8000)

 SELECT @res = COALESCE(@res +',','') + fname

   FROM foodtable

  WHERE fname IS NOT NULL

  GROUP BY fname

 RETURN @res

END

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22822169
and your query will be:
select  fid, dbo.ConcatFoodName(fid)

  from maintable

Open in new window

0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 22823565
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22823994
Thanks for help ..
CREATE FUNCTION dbo.ConcatFoodName1(@rid int)
RETURNS VARCHAR(8000)
AS
BEGIN
 DECLARE @res VARCHAR(8000)
 SELECT @res = COALESCE(@res +',','') + resname
   FROM resttable
  WHERE resname is nOT NULL
  GROUP BY resname
 RETURN @res
END


select  fid, dbo.ConcatFoodName1(rid)  from maintable


Result displays like dis..
1    Restaurant A,Restaurant B,Restaurant C,Restaurant D
1    Restaurant A,Restaurant B,Restaurant C,Restaurant D
1    Restaurant A,Restaurant B,Restaurant C,Restaurant D
4    Restaurant A,Restaurant B,Restaurant C,Restaurant D
5    Restaurant A,Restaurant B,Restaurant C,Restaurant D
9    Restaurant A,Restaurant B,Restaurant C,Restaurant D
9    Restaurant A,Restaurant B,Restaurant C,Restaurant D
11    Restaurant A,Restaurant B,Restaurant C,Restaurant D
3    Restaurant A,Restaurant B,Restaurant C,Restaurant D
1    Restaurant A,Restaurant B,Restaurant C,Restaurant D
4    Restaurant A,Restaurant B,Restaurant C,Restaurant D


1.But i need to display only one time of every fid .. in the above result it shows fid 1 many times ..
 2.if fid should  as only 2 restaurants its should not displays other restaurants. i.e for eg fid 4 as restaurant a , restaurant b should not show restaurant c, restaurant d ...here it displays every restaurant exiists in the d table.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22824621
group by:
select  fid, dbo.ConcatFoodName1(rid)  from maintable

group by fid

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22824635
and the function is missing the where clause:
CREATE FUNCTION dbo.ConcatFoodName(@rid int)

RETURNS VARCHAR(8000)

AS

BEGIN

 DECLARE @res VARCHAR(8000)

 SELECT @res = COALESCE(@res +',','') + fname

   FROM foodtable

  WHERE fname IS NOT NULL

    AND rid = @rid

  GROUP BY fname

 RETURN @res

END

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22827854
thanks , it very very closer to my output,


While executing below sttmt am getting error,
select   fid,dbo.resname(rid)  from maintablegroup by fid
Column 'maintable.rid' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

But without groupby clause , i got the below output.
the result was
1    Restaurant A
1    Restaurant B
1    Restaurant C
4    Restaurant D
5    Restaurant B
9    Restaurant C
9    Restaurant A
11 Restaurant D
3    Restaurant D
1    Restaurant D
4    Restaurant B

But i need to display like this ,
fid                  resname1                     RestaurantA,RestaurantB,RestaurantC,RestaurantD3                     RestaurantD
4                     RestaurantA,RestaurantB
5                      RestaurantB9                       RestaurantA, RestaurantC
11                     RestaurantD

This is output i want,
I changed the code according to retrieve resname from resttable . kingly suggest.


CREATE FUNCTION dbo.resname(@rid int)

RETURNS VARCHAR(8000)

AS

BEGIN

 DECLARE @res VARCHAR(8000)

 SELECT @res = COALESCE(@res +',','') + resname

   FROM resttable

  WHERE resname IS NOT NULL

    AND rid = @rid

  GROUP BY resname

 RETURN @res

END

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22829930
what about this:
select   fid,dbo.resname(rid)  from maintable

group by fid , rid

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now