Solved

sql server 2000  Incorrect syntax near 'xml'.

Posted on 2008-10-28
10
1,257 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

756 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