473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

For XML clause

I'm trying to turn a product table into an XML file to create an
heirarchical menu, and I was wondering if there was any easy way to do
this. The table is (simplified) below:

Create table product(
category varchar,
subcategory varchar,
name varchar)

and I want the XML to represent the category structure as below:

<Categories>
<sucategories>
<products>
.....
</products>
</subcategories>
</Categories>

Thanks,

John

Jul 23 '05 #1
3 1422
(jh******@yahoo.com) writes:
I'm trying to turn a product table into an XML file to create an
heirarchical menu, and I was wondering if there was any easy way to do
this. The table is (simplified) below:

Create table product(
category varchar,
subcategory varchar,
name varchar)

and I want the XML to represent the category structure as below:

<Categories>
<sucategories>
<products>
.....
</products>
</subcategories>
</Categories>


I think that you should be able to do this with FOR XML EXPLICIT.
But I'm not very good at XML queries, so I don't want give an outline of
something that is likely to be incorrect.

If you post a CREATE TABLE statement for the table, INSERT statements
for some sample data, and the desired XML from the data, I might give
it a try.

Or you could look at FOR XML yourself in Books Online. (That's anyway
where I have to look to write this type of queries. :-)

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
Create table product(
category varchar(10),
subcategory varchar(10),
name varchar(10))

insert into product(category,subcategory,name)
values ('Vehicle','Car','Sports')
insert into product(category,subcategory,name)
values ('Vehicle','Car','Saloon')
insert into product(category,subcategory,name)
values ('Vehicle','Motorbike','Sports')
insert into product(category,subcategory,name)
values ('Vehicle','Motorbike','Tourer')

dbcc traceon(257) -- pretty print for Query Analyzer text results

select distinct 1 as Tag,
null as Parent,
category as [Category!1!Value],
null as [subcategories!2!Value],
null as [products!3!Value]
from product
union all
select distinct 2 as Tag,
1 as Parent,
category as [Category!1!Value],
subcategory as [subcategories!2!Value],
null as [products!3!Value]
from product
union all
select 3 as Tag,
2 as Parent,
category as [Category!1!Value],
subcategory as [subcategories!2!Value],
name as [products!3!Value]
from product
order by [Category!1!Value],[subcategories!2!Value],[products!3!Value]
for xml explicit

Jul 23 '05 #3
(ma******@hotmail.com) writes:
dbcc traceon(257) -- pretty print for Query Analyzer text results


Ah, that's one cute. Didn't know.

And thanks for posting the example!
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
2
by: aj70000 | last post by:
This is my query select ano,max(date),a_subject from MY_TAB where table_name='xyz' and ano=877 group by a_subject,ano order by a_subject ANO max(Date) A_Subject 877 2005-01-20...
7
by: JJ_377 | last post by:
Can someone tell me why SQL seems to ignore my order by clause? I tried to run through the debugger, but the debugger stops at the select statement line and then returns the result set; so, I have...
27
by: Chris, Master of All Things Insignificant | last post by:
I have come to greatly respect both Herfried & Cor's reponses and since the two conflicted, I wanted to get some clarification. My orginal post: Herfried, maybe your example here can get you to...
3
by: Sean Shanny | last post by:
To all, We are running postgresql 7.4.1 on an G5 with dual procs, OSX 10.3.3 server, 8GB mem, attached to a fully configured 3.5TB XRaid box via fibre channel. I think we have run into this...
26
by: GreatAlterEgo | last post by:
Hi, This is my query which is embedded in a COBOL program. EXEC SQL SELECT DATE, AGE, DURATION, AMT INTO :LDATE, :L.AGE, :L.DURATION, :L.AMT FROM TAB1 WHERE CODE = :KEY.CODE AND...
25
by: metaperl.etc | last post by:
A very old thread: http://groups.google.com/group/comp.lang.python/browse_frm/thread/2c5022e2b7f05525/1542d2041257c47e?lnk=gst&q=for+else&rnum=9#1542d2041257c47e discusses the optional "else:"...
2
by: Jim.Mueksch | last post by:
I am having a problem with using calculated values in a WHERE clause. My query is below. DB2 gives me this error message: Error: SQL0206N "APPRAISAL_LESS_PRICE" is not valid in the context where...
5
by: pwiegers | last post by:
Hi, I'm trying to use the result of a conditional statement in a where clause, but i'm getting 1)nowhere 2) desperate :-) The query is simple: -------- SELECT idUser,...
6
by: jackal_on_work | last post by:
Hi Faculties, I have two queries which give me the same output. -- Query 1 SELECT prod.name, cat.name FROM products prod INNER JOIN categories cat ON prod.category_id = cat.id WHERE cat.id...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.