Solved

sql server 2000  Incorrect syntax near 'xml'.

Posted on 2008-10-28
10
1,240 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 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 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

809 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