User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 361,552 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,139 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 435 | Replies: 0
Reply
Join Date: Apr 2008
Posts: 2
Reputation: sam aspin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sam aspin sam aspin is offline Offline
Newbie Poster

Sql for xml explicit - help needed!

  #1  
Apr 29th, 2008
Hi, can anybody shed any light on what I'm doing wrong here...

I'm trying to acheive an XML that looks like the following:
<tshirt Name="Stussy NY Posse T-Shirt Yellow">
  <variant size="small">
    <productID>10</productID>
    <dimensions length="34" />
  </variant>
  <variant size="medium">
    <productID>11</productID>
    <dimensions length="45" />
  </variant>
  <variant size="large">
    <productID>12</productID>
    <dimensions length="50" />
  </variant>
</tshirt>


Unfortunately, what I'm actually getting is this:
<tshirt Name="Stussy NY Posse T-Shirt Yellow">
  <variant size="small">
    <productID>10</productID>
  </variant>
  <variant size="medium">
    <productID>11</productID>
  </variant>
  <variant size="large">
    <productID>12</productID>
    <dimensions length="34" />
    <dimensions length="45" />
    <dimensions length="50" />
  </variant>
</tshirt>

As you will no doubt notice, all the lengths appear in the 'large' variant when each one is actually associated with the other sizes.

The SQL I'm running is as follows:

declare @productid int
set @productid = 5

SELECT 1 as Tag,
NULL as Parent,
pt.Name	as [tshirt!1!Name],
null	as [variant!2!size],
null	as [variant!2!productID!element],
null	as [variant!2!dimensions!element],
null	as [dimensions!3!length]

from producttemplates pt
where pt.ptid = @productid

UNION ALL

SELECT 2 as Tag,
1 as Parent,
NULL	as [tshirt!1!Name],
s.Size	as [variant!2!size],
p.pid	as [variant!2!productID!element],
null	as [variant!2!dimensions!element],
null	as [dimensions!3!length!element]

from sizes s inner join products p on p.sizeid = s.sizeid
inner join producttemplates pt on p.ptid = pt.ptid
where pt.ptid = @productid

union all
select 3 as Tag,
2 as Parent,
null as [tshirt!1!Name],
null as [variant!2!size],
null as [variant!2!productID!element],
null as [variant!2!dimensions!element],
pDim.ShirtLength as [dimensions!3!length!element]

from ProductDimensions pDim
INNER JOIN products p on p.pid = pDim.pid
where p.ptid = @productid

for xml explicit


ANY HELP WOULD BE MASSIVELY APPRECIATED!
AddThis Social Bookmark Button
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 2:11 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC