Solved

sql server 2000  Incorrect syntax near 'xml'.

Posted on 2008-10-28
10
1,273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 143

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 143

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 143

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 70

Expert Comment

by:Éric Moreau
ID: 22823565
0
 
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 143

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 143

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 143

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

726 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