FOR XML EXPLICIT:创建多个结果行
FOR XML EXPLICIT: Create multiple result rows
我想在一个语句中创建多个 XML Files/Result 行。
我有:
with invoices as (
select '1' HID, 'Test1' HData, '1' PID, 'Pos1' PData union
select '1' HID, 'Test1' HData, '2' PID, 'Pos2' PData union
select '1' HID, 'Test1' HData, '3' PID, 'Pos3' PData union
select '2' HID, 'Test2' HData, '4' PID, 'Pos1' PData union
select '2' HID, 'Test2' HData, '5' PID, 'Pos2' PData union
select '2' HID, 'Test2' HData, '6' PID, 'Pos3' PData
)
select
case when GROUPING_ID(PID) = 0 then 4
when GROUPING_ID(HData) = 0 then 3
when GROUPING_ID(HID) = 0 then 2
else 1 end tag,
case when GROUPING_ID(PID) = 0 then 2
when GROUPING_ID(HData) = 0 then 2
when GROUPING_ID(HID) = 0 then 1
else null end parent,
null [Root!1],
HID [Invoice!2!HID],
HData [Header!3!HData!Element],
PID [Pos!4!PID],
PData [Pos!4!PData!Element]
from invoices
group by grouping sets ((), (HID), (HID, HData), (HID, HData, PID, PData))
order by HID, HData, PID
for xml explicit, type;
我得到的:
我想要的:
这可能吗?怎么做到的?
您只需为每个不同的 HID
:
生成 XML
输出
;WITH invoices as (
select '1' HID, 'Test1' HData, '1' PID, 'Pos1' PData union
select '1' HID, 'Test1' HData, '2' PID, 'Pos2' PData union
select '1' HID, 'Test1' HData, '3' PID, 'Pos3' PData union
select '2' HID, 'Test2' HData, '4' PID, 'Pos1' PData union
select '2' HID, 'Test2' HData, '5' PID, 'Pos2' PData union
select '2' HID, 'Test2' HData, '6' PID, 'Pos3' PData
)
SELECT
XMLColumn = (
select
case when GROUPING_ID(PID) = 0 then 4
when GROUPING_ID(HData) = 0 then 3
when GROUPING_ID(HID) = 0 then 2
else 1
end tag,
case when GROUPING_ID(PID) = 0 then 2
when GROUPING_ID(HData) = 0 then 2
when GROUPING_ID(HID) = 0 then 1
else null
end parent,
null [Root!1],
HID [Invoice!2!HID],
HData [Header!3!HData!Element],
PID [Pos!4!PID],
PData [Pos!4!PData!Element]
from invoices
WHERE i.HID = HID
group by grouping sets ((), (HID), (HID, HData), (HID, HData, PID, PData))
order by HID, HData, PID
for xml explicit, type
)
FROM invoices i
GROUP BY HID
我想在一个语句中创建多个 XML Files/Result 行。
我有:
with invoices as (
select '1' HID, 'Test1' HData, '1' PID, 'Pos1' PData union
select '1' HID, 'Test1' HData, '2' PID, 'Pos2' PData union
select '1' HID, 'Test1' HData, '3' PID, 'Pos3' PData union
select '2' HID, 'Test2' HData, '4' PID, 'Pos1' PData union
select '2' HID, 'Test2' HData, '5' PID, 'Pos2' PData union
select '2' HID, 'Test2' HData, '6' PID, 'Pos3' PData
)
select
case when GROUPING_ID(PID) = 0 then 4
when GROUPING_ID(HData) = 0 then 3
when GROUPING_ID(HID) = 0 then 2
else 1 end tag,
case when GROUPING_ID(PID) = 0 then 2
when GROUPING_ID(HData) = 0 then 2
when GROUPING_ID(HID) = 0 then 1
else null end parent,
null [Root!1],
HID [Invoice!2!HID],
HData [Header!3!HData!Element],
PID [Pos!4!PID],
PData [Pos!4!PData!Element]
from invoices
group by grouping sets ((), (HID), (HID, HData), (HID, HData, PID, PData))
order by HID, HData, PID
for xml explicit, type;
我得到的:
我想要的:
这可能吗?怎么做到的?
您只需为每个不同的 HID
:
XML
输出
;WITH invoices as (
select '1' HID, 'Test1' HData, '1' PID, 'Pos1' PData union
select '1' HID, 'Test1' HData, '2' PID, 'Pos2' PData union
select '1' HID, 'Test1' HData, '3' PID, 'Pos3' PData union
select '2' HID, 'Test2' HData, '4' PID, 'Pos1' PData union
select '2' HID, 'Test2' HData, '5' PID, 'Pos2' PData union
select '2' HID, 'Test2' HData, '6' PID, 'Pos3' PData
)
SELECT
XMLColumn = (
select
case when GROUPING_ID(PID) = 0 then 4
when GROUPING_ID(HData) = 0 then 3
when GROUPING_ID(HID) = 0 then 2
else 1
end tag,
case when GROUPING_ID(PID) = 0 then 2
when GROUPING_ID(HData) = 0 then 2
when GROUPING_ID(HID) = 0 then 1
else null
end parent,
null [Root!1],
HID [Invoice!2!HID],
HData [Header!3!HData!Element],
PID [Pos!4!PID],
PData [Pos!4!PData!Element]
from invoices
WHERE i.HID = HID
group by grouping sets ((), (HID), (HID, HData), (HID, HData, PID, PData))
order by HID, HData, PID
for xml explicit, type
)
FROM invoices i
GROUP BY HID