1

I have an xml which looks like this. <name_row> nodes has the column names and <value_row> has all the values. How do I cross join?

Declare @myXML xml
SET @myXML = '<root>
 <name_row>
  <column>  
     <name>CustomerID</name>
   </column>
     <column>  
     <name>ProductID</name>
   </column>
   <column>  
     <name>Price</name>
   </column>
</name_row>
<value_row>  
   <column>  
     <value>123</value>
   </column>
    <column>  
     <value>101</value>
   </column> 
    <column>  
     <value>$12.00</value>
   </column>      
</value_row>
<value_row>  
   <column>  
     <value>123</value>
   </column>
    <column>  
     <value>102</value>
   </column> 
    <column>  
     <value>$15.00</value>
   </column>      
</value_row>
</root>'

I need to save each values in a different column fields.

select x.i.value('(./text()[1]', 'varchar(max)')
from @myXML x 
cross apply x.nodes('/root/value_row/column/value') as x(i)

Thanks

1 Answer 1

1

Try something like

select v.value('(column/value)[1]', 'varchar(max)') as CustomerID
     , v.value('(column/value)[2]', 'varchar(max)') as ProductID
     , v.value('(column/value)[3]', 'varchar(max)') as Price
into #tempTable
from @myXML.nodes('root/value_row') t(v)

select * from #tempTable

drop table #tempTable
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.