Note that there are some explanatory texts on larger screens.

plurals
  1. POGet XML data in to table
    text
    copied!<p>i have following XML </p> <pre><code>&lt;Queue&gt; &lt;UserName&gt;UserName&lt;/UserName&gt; &lt;Type&gt;1&lt;/Type&gt; &lt;Portal name="Portal1"&gt; &lt;IndexesForSelect&gt; &lt;Index&gt;Index1&lt;/Index&gt; &lt;Index&gt;Index2&lt;/Index&gt; &lt;/IndexesForSelect&gt; &lt;/Portal&gt; &lt;Portal name="Portal2"&gt; &lt;IndexesForSelect&gt; &lt;Index&gt;Index3&lt;/Index&gt; &lt;Index&gt;Index4&lt;/Index&gt; &lt;/IndexesForSelect&gt; &lt;/Portal&gt; &lt;/Queue&gt; </code></pre> <p>and i need to get it to a table in below format</p> <pre><code>Portal Index --------------------------- Portal1 Index1 Portal1 Index2 Portal2 Index3 Portal2 Index4 </code></pre> <p>I really appreciate if anyone can help me.</p> <p>I have tried the following code but it only returns first <code>Index</code> of every <code>Portal</code></p> <pre><code>declare @T table ( XMLCol xml ) insert into @T values ('&lt;Queue&gt; &lt;UserName&gt;UserName&lt;/UserName&gt; &lt;Type&gt;1&lt;/Type&gt; &lt;Portal name="Portal1"&gt; &lt;IndexesForSelect&gt; &lt;Index&gt;Index1&lt;/Index&gt; &lt;Index&gt;Index2&lt;/Index&gt; &lt;/IndexesForSelect&gt; &lt;/Portal&gt; &lt;Portal name="Portal2"&gt; &lt;IndexesForSelect&gt; &lt;Index&gt;Index3&lt;/Index&gt; &lt;Index&gt;Index4&lt;/Index&gt; &lt;/IndexesForSelect&gt; &lt;/Portal&gt; &lt;/Queue&gt;') SELECT items.value('../@name','varchar(max)') AS [Portal], items.value('(Index)[1]','varchar(max)') AS [Index] FROM @T AS T CROSS APPLY T.XMLCol.nodes('Queue/Portal/IndexesForSelect') c (items) </code></pre>
 

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