Hi All,
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.
Here is an example of the script that works, but is not dynamic.
Script:
DECLARE @XML XML = '<root>
<data>
<list id="organization" label="Organization">
<options>
<item value="1" label="Organization1" selected="false" />
<item value="2" label="Organization2" selected="false" />
<item value="3" label="Organization3" selected="false" />
<item value="4" label="Organization4" selected="true" />
<item value="5" label="Organization5" selected="true" />
</options>
</list>
</data>
</root>';
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
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:
Insert Into @Orgs(ID) Select OrgNameIdNodes.ID.value(@Value) from @xml.nodes(@Nodes) as OrgNameIdNodes(ID);
Is this possible?