Solved

sql server 2000  Incorrect syntax near 'xml'.

Posted on 2008-10-28
10
1,224 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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 extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

747 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

8 Experts available now in Live!

Get 1:1 Help Now