I am trying to generate some XML using TSQL and having some trouble, I was able to get to traverse 2 levels but getting stuck at 3. How it should work in the example I have posted you have to Purchase Orders, the first purchase order only has one part with one release for that part. The second Purchase order as 2 parts each part with its own release. At the end I should have 2 XML documents that traverse 3 levels, release/part/purchase order. I think I am getting confused when linking to the next stage up as well as the group by.The example only shows 2 parts but there can be many parts to one purchase order but only ever one release per part, any help would be much appreciated.
DECLARE @sdv AS TABLE
(
PID INT IDENTITY(1,1) PRIMARY KEY,
PurchaseOrder varchar(20),
PartNo int,
Release int
)
INSERT INTO @sdv values ('PURC123', 111, 1)
INSERT INTO @sdv values ('PURC333', 222, 1)
INSERT INTO @sdv values ('PURC333', 333, 1)
select
PurchaseOrder,
(
select
PartNo,
(
select
Release
from
@sdv a3
where
a3.Release = a2.Release and
a3.PartNo = a2.PartNo and
a3.PurchaseOrder = a1.PurchaseOrder
for xml path('Release'), type
)
from
@sdv a2
where
a2.PartNo = a1.PartNo
group by a2.PartNo
for xml path('part'), type
) from @sdv a1
group by PurchaseOrder
for xml path('Purchase')
I am going to shoot you an answer so you can play around with it. I am around for a little bit longer today, but will check back tomorrow.
For this example I just used attributes as I can read it a bit easier. I did my grouping using a CTE and assumed the relationship was 1 to many between a part and a release. (could be wrong there, but need info from you). Was also a bit hazy about the levels needing to go a third deep. If we are talking about iterations of parts (versions) then you could simple add a @Release to the second level. Note: If you don't want attributes just remove the @before each of the tags.
I would highly recommend being very diligent about your code format whenever doing hierarchical xml work. Its easy to get lost.
CODE
DECLARE @sdv AS TABLE
(
PID INT IDENTITY(1,1) PRIMARY KEY,
PurchaseOrder varchar(20),
PartNo int,
Release int
)
INSERT INTO @sdv values ('PURC123', 111, 1)
INSERT INTO @sdv values ('PURC333', 222, 1)
INSERT INTO @sdv values ('PURC333', 333, 1)
;WITH PO AS (
SELECT DISTINCT PurchaseOrder
FROM @sdv
)
SELECT L1.PurchaseOrder AS "@PurchseOrder",
( SELECT PartNo AS "@PartNo",
( SELECT DISTINCT L3.Release AS "@Release"
FROM @sdv AS L3
WHERE L2.PartNo = L3.PartNo
ORDER BY L3.Release ASC
FOR XML PATH('RELEASE'),TYPE
)
FROM @sdv AS L2
WHERE L2.PurchaseOrder = L1.PurchaseOrder
FOR XML PATH('PART'), TYPE
)
FROM PO AS L1
FOR XML PATH('PurchaseOrder'), ROOT('PurchaseOrders')
XML OUTPUT
<PurchaseOrders>
<PurchaseOrder PurchseOrder="PURC123">
<PART PartNo="111">
<RELEASE Release="1" />
</PART>
</PurchaseOrder>
<PurchaseOrder PurchseOrder="PURC333">
<PART PartNo="222">
<RELEASE Release="1" />
</PART>
<PART PartNo="333">
<RELEASE Release="1" />
</PART>
</PurchaseOrder>
</PurchaseOrders>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.