There're a several issues here. First, your xml looks like this:
<myroot>
<Test>
<A>1</A><B>11</B>
</Test>
<Test>
<A>4</A><B>44</B>
</Test>
<Test>
<A>5</A><B>55</B>
</Test>
</myroot>
And you're trying to fetch data as attributes (@A
, @B
). You need to fetch it as elements (A[1]
or (A/text())[1]
).
Second, you have to use type
keyword if you want your xml
to be xml type.
Third, to split data by rows you need nodes()
function. So your query becomes:
select
D.Z.value('(A/text())[1]', 'int'),
D.Z.value('(B/text())[1]', 'int')
from (
select A, B
from Test
order by A
for xml auto, elements, root('myroot'), type
) as Doc(Z)
outer apply Doc.Z.nodes('myroot/Test') as D(Z)
BTW, I'd better to use attributes, like this:
select
D.Z.value('@A', 'int'),
D.Z.value('@B', 'int')
from (
select A, B
from Test
order by A
for xml raw('Test'), root('myroot'), type
) as Doc(Z)
outer apply Doc.Z.nodes('myroot/Test') as D(Z)