views:

32

answers:

1

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?

+1  A: 

How about using sp_executesql with dynamic sql. Something like:

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);

declare @nodes nvarchar(4000),
        @value nvarchar(4000),
        @query nvarchar(4000)

select @value = '''@value'',''int'''
select @nodes = '//*[@id="organization"]//item[@selected="true"]'

select @query = 'Select OrgNameIdNodes.ID.value( ' + @value + ') ' +
                'from @xml.nodes(''' + @nodes + ''') as OrgNameIdNodes(ID)'

insert into @Orgs(ID) EXEC sp_executesql @query, N'@xml xml', @xml = @xml

Select * 
from @orgs
Garett
Perfect thanks Garett
scarpacci