3

I have three column in my table with one column of XML type. I want to show different nodes of the XML data in different rows. Like,

Col1      Col2         Col3
12        ABC          <Interactions><interaction id='2' name='A'/><interaction id='3' name='B'/></Interactions> 

How to show different interaction in different rows? like,

Row1 :  12    ABC   2   A
Row2 :  12    ABC   3   B

Thanks! Ank

Roman Pekar
  • 107,110
  • 28
  • 195
  • 197
CODError
  • 779
  • 2
  • 10
  • 25

2 Answers2

2

Try this

SELECT 
    A.Col1
    ,A.Col2
    ,Split.interaction.value('@id', 'INT') AS [ID]
    ,Split.interaction.value('@name', 'VARCHAR(10)') AS [Name]
 FROM  
 (
    SELECT Col1, Col2, Col3 FROM  #Tablexml
 ) AS A CROSS APPLY Col3.nodes ('/Interactions/interaction') AS Split(interaction);

DEMO

Community
  • 1
  • 1
bvr
  • 4,786
  • 1
  • 20
  • 24
0
select
    T.Col1, T.Col2,
    C.N.value('@id', 'int') as ID,
    C.N.value('@name', 'nvarchar(max)') as Name
from Table1 as T
    outer apply T.Col3.nodes('Interactions/interaction') as C(N)

sql fiddle demo

Roman Pekar
  • 107,110
  • 28
  • 195
  • 197