Note that there are some explanatory texts on larger screens.

plurals
  1. POXQuery and Node Ids
    primarykey
    data
    text
    <p>I have this variable:</p> <pre><code>declare @xmlDoc XML </code></pre> <p>it has the following xml stored in it:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;NewDataSet&gt; &lt;Table1&gt; &lt;Sharedparam&gt;shared&lt;/Sharedparam&gt; &lt;Antoher&gt;sahre&lt;/Antoher&gt; &lt;RandomParam2&gt;Good stuff&lt;/RandomParam2&gt; &lt;MoreParam&gt;and more&lt;/MoreParam&gt; &lt;ResultsParam&gt;2&lt;/ResultsParam&gt; &lt;/Table1&gt; &lt;Table1&gt; &lt;RandomParam2&gt;do you&lt;/RandomParam2&gt; &lt;MoreParam&gt;think&lt;/MoreParam&gt; &lt;ResultsParam&gt;2&lt;/ResultsParam&gt; &lt;/Table1&gt; &lt;Table1&gt; &lt;Sharedparam&gt;Last&lt;/Sharedparam&gt; &lt;Antoher&gt; Set &lt;/Antoher&gt; &lt;RandomParam2&gt; of &lt;/RandomParam2&gt; &lt;MoreParam&gt;values&lt;/MoreParam&gt; &lt;ResultsParam&gt;are here&lt;/ResultsParam&gt; &lt;/Table1&gt; &lt;Table1 /&gt; &lt;/NewDataSet&gt; </code></pre> <p>I have this query that I am using to get the data:</p> <pre><code>declare @xmlDoc XML set @xmlDoc = '' -- Stack Overflow could not handle the xml all on one line. SELECT -- Param 1 TBL.SParam.value('local-name((*)[1])', 'varchar(50)') as Param1Name, TBL.SParam.value('(*)[1]', 'varchar(100)') as Param1Value, -- Param2 TBL.SParam.value('local-name((*)[2])', 'varchar(50)') as Param2Name, TBL.SParam.value('(*)[2]', 'varchar(100)') as Param2Value, -- Param3 TBL.SParam.value('local-name((*)[3])', 'varchar(50)') as Param3Name, TBL.SParam.value('(*)[3]', 'varchar(100)') as Param3Value, -- Param 4 TBL.SParam.value('local-name((*)[4])', 'varchar(50)') as Param4Name, TBL.SParam.value('(*)[4]', 'varchar(100)') as Param4Value, -- Param 5 TBL.SParam.value('local-name((*)[5])', 'varchar(50)') as Param5Name, TBL.SParam.value('(*)[5]', 'varchar(100)') as Param5Value FROM @xmldoc.nodes('/NewDataSet/Table1') AS TBL(SParam) </code></pre> <p>I need a way to add to my results the order that they came from the xml file. (Which was the first instance of Table1, then the second....).</p> <p>For reasons of <a href="https://stackoverflow.com/questions/6471210/is-there-anyway-to-reset-the-identity-of-a-table-variable">SQL table variable limitations</a>, I can't use an identity column to keep this straight. (For other reasons, I don't want to use a temporary table.) </p> <p>I am hoping that there is a cool SQL XML function that will return some kind of internally assigned Node ID. (Or some other similar manner of ordering.)</p> <p>Note, I do not control this XML Structure (I am a reader only) so I cannot make changes to add in an ID attribute.</p> <p>Any advice would be great!</p> <p><strong>EDIT/Update:</strong></p> <p>I would really like to have this data like this:</p> <pre> 1 | SharedParam | shared 1 | Antoher | sahre 1 | RandomParam2 | Good stuff 1 | MoreParam | and more 1 | ResultsParam | and more 2 | RandomParam2 | do you 2 | MoreParam | think 2 | ResultsParam | 2 3 | Sharedparam | Last 3 | Antoher | Set . . . </pre> <p>But I am coming up short. I can get it into columns (more or less), but I don't know how to do the numbering. If you have any ideas I would love to hear them.</p> <p><strong>EDIT:</strong><br> I figured out the query to do this (with some help from the internet). It looks like this:</p> <pre><code>SELECT TBL.SParam.value('local-name(.)[1]', 'varchar(50)') as ParamName, TBL.SParam.value('(.)[1]', 'varchar(50)') ParamValue, TBL.SParam.value('for $s in . return count(../*[. &lt;&lt; $s]) + 1', 'int') ParamPosition, TBL.SParam.value('for $s in . return count(../../*[. &lt;&lt; $s]) - 1', 'int') ParamIteration FROM @xmldoc.nodes('/NewDataSet/Table1/*') AS TBL(SParam) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload