title: T-SQL Dynamic xquery
body: <p>I am trying to figure out how I can load my table variable with data from XML using dynamic xquery? I am getting a result set of nodes from the query and defining the value type of those nodes.  It seems that it is the value definition of the nodes that it is blowing up on.  </p>
  
  <p>Here is an example of the script that works, but is not dynamic.</p>
  
  <p>Script:</p>
  
  <pre><code>DECLARE @XML XML = '&lt;root&gt;
                        &lt;data&gt;
                           &lt;list id="organization" label="Organization"&gt;
                                    &lt;options&gt;
                                      &lt;item value="1" label="Organization1" selected="false" /&gt;
                                      &lt;item value="2" label="Organization2" selected="false" /&gt;
                                      &lt;item value="3" label="Organization3" selected="false" /&gt;
                                      &lt;item value="4" label="Organization4" selected="true" /&gt;
                                      &lt;item value="5" label="Organization5" selected="true" /&gt;
                                    &lt;/options&gt;
                            &lt;/list&gt;
                        &lt;/data&gt;
                      &lt;/root&gt;';
  
  
  DECLARE @Orgs       TABLE (ID INT);
  
  Insert Into @Orgs(ID) Select OrgNameIdNodes.ID.value('@value','int') from @xml.nodes('//*[@id="organization"]//item[@selected="true"]') as OrgNameIdNodes(ID);
  
  Select *
  from @orgs
  </code></pre>
  
  <p>What I would like to be able to do is pass in parameters for both value and the @xml.nodes sections so I would have something like:</p>
  
  <pre><code>Insert Into @Orgs(ID) Select OrgNameIdNodes.ID.value(@Value) from @xml.nodes(@Nodes) as     OrgNameIdNodes(ID);
  </code></pre>
  
  <p>Is this possible?</p>