Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL data int XML multiple elements needed

Status
Not open for further replies.

shpinoza1980

Programmer
Oct 18, 2010
1
IL
Hi, I'm trying to create an xml document out of a sql 2005 table

I use this code in a query:

select p.name as "NAME"
from nop_product p
for xml path('PRODUCT'),ROOT('STORE')

my problem is that i need 3 level tree - all <PRODUCT> elements to be under <PRODUCTS> like this:
<STORE>
<PRODUCTS>
<PRODUCT>
<NAME> ..... </NAME>
</PRODUCT>
<PRODUCT>
<NAME> ....... </NAME>
</PRODUCT>
</PRODUCTS>
</STORE>

how do i do that? how do I add another permanent element after the ROOT tag and before the <PRODUCT> tag?
thanks
 
You can do this.
[tt]
select (select
p.name as 'NAME'
from nop_product p
for xml path('PRODUCT'), TYPE, ROOT('PRODUCTS')
) for xml path(''), ROOT('STORE')
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top