Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does nodes() or openxml returns rows in same order as it finds in xml?

I have an xml which i need to parse using openxml or nodes(). The xml contains few child tags that repeat with different values, as below.

<root>
    <value>10</value>
    <value>12</value>
    <value>11</value>
    <value>1</value>
    <value>15</value>
<root>

For my code it is very important that i get all these rows returned in same order as in xml. I googled and gogled but nothing tells me if the @mp:id is always returned in same order as in xml. Or if nodes() return values in same order as it encounters them.

All I want to know if I can trust any of those two methods and be happy with proper order of rows.

P.S. excuse any errors or mistakes in above text, I dont enjoy typing codes in an android window either.

like image 271
Kanwal Sarwara Avatar asked Aug 21 '12 20:08

Kanwal Sarwara


People also ask

What is the purpose of Openxml clause SQL Server stored procedure?

The OPENXML function allows the data in a XML document to be treated just like the columns and rows of your database table. The function is used with the sp_xml_preparedocument stored system procedure.

How do I query values in XML nodes?

You can retrieve multiple values from the rowset. For example, you can apply the value() method to the rowset returned by nodes() and retrieve multiple values from the original XML instance. The value() method, when applied to the XML instance, returns only one value.

What is Openxml in SQL Server?

OPENXML provides a rowset view over an XML document. Because OPENXML is a rowset provider, OPENXML can be used in Transact-SQL statements in which rowset providers such as a table, view, or the OPENROWSET function can appear.

How do I query XML data in SQL?

SQL Server provides the XQuery feature to querying XML data type or querying with the XML column with the XPATH. Using XQuery, users can Insert, Update and Delete with the XML nodes and node values in an XML column.


1 Answers

You can use row_number on the shredded XML like this.

declare @XML xml= 
'<root>
    <value>10</value>
    <value>12</value>
    <value>11</value>
    <value>1</value>
    <value>15</value>
</root>'

select value
from
  (
  select T.N.value('.', 'int') as value,
         row_number() over(order by T.N) as rn 
  from @xml.nodes('/root/value') as T(N)
  ) as T
order by T.rn

Uniquely Identifying XML Nodes with DENSE_RANK

Update:

You can also use a numbers table like this;

declare @XML xml= 
'<root>
    <value>10</value>
    <value>12</value>
    <value>11</value>
    <value>1</value>
    <value>15</value>
</root>';

with N(Number) as
(
  select Number
  from master..spt_values
  where type = 'P'
)
select @XML.value('(/root/value[sql:column("N.Number")])[1]', 'int')
from N
where N.Number between 1 and @XML.value('count(/root/value)', 'int')
order by N.Number
like image 130
Mikael Eriksson Avatar answered Oct 07 '22 18:10

Mikael Eriksson