2
Hello,
How can I select one XML
with header and items?
I have it:
DECLARE @XML XML = CONVERT(XML,'
<ROOT v="2.15.0">
<LOTEANIH APP="00B1" LOTE="1">
<LOTEANIM ANIMAL="10" />
<LOTEANIM ANIMAL="11" />
<LOTEANIM ANIMAL="12" />
<LOTEANIM ANIMAL="13" />
<LOTEANIM ANIMAL="14" />
</LOTEANIH>
<LOTEANIH APP="XB8F" LOTE="1">
<LOTEANIM ANIMAL="1" />
<LOTEANIM ANIMAL="2" />
<LOTEANIM ANIMAL="3" />
<LOTEANIM ANIMAL="4" />
<LOTEANIM ANIMAL="5" />
</LOTEANIH>
</ROOT>');
I’m selecting this way (I know it’s wrong :/):
WITH CTe AS (
SELECT
h.value('@APP', 'varchar(4)') AS APP_ID
,h.value('@LOTE', 'int') AS NRO_LOTE
,i.value('@ANIMAL', 'int') AS ANIMAL
FROM @XML.nodes('/ROOT/LOTEANIH') AS header(h)
CROSS APPLY @XML.nodes('/ROOT/LOTEANIH/LOTEANIM') AS child(i)
)
SELECT * FROM CTe