Given the following sample of XML and the select statement that shreds the xml into a relation, what I need is the second column of the select to be the ordinal of the category (ie 1 for the directions and 2 for the colours in this case).
Note: The literal value ‘rank()’ in the select is left a placeholder. I was poking around with using the rank, but with no success.
declare @x xml
set @x = '
<root>
<category>
<item value="north"/>
<item value="south"/>
<item value="east"/>
<item value="west"/>
</category>
<category>
<item value="red"/>
<item value="green"/>
<item value="blue"/>
</category>
</root>'
select c.value('./@value', 'varchar(10)') as "ItemValue",
'rank()' as "CategoryNumber"
from @x.nodes('//item') as t(c)
Jacob Sebastian also has an interesting solution presented in his blog post:
XQuery Lab 23 – Retrieving values and position of elements
With Jacob’s suggestion, I can rewrite your query to be:
and I get the desired output:
Unfortunately, none of the more obvious solutions like the
position()orfn:id()functions seem to a) work in SQL Server or b) be supported in SQL Server at all 🙁Hope this helps
Marc