Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL server : How to seperate XML nodes in different rows

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

like image 369
CODError Avatar asked Feb 14 '26 08:02

CODError


1 Answers

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

like image 159
bvr Avatar answered Feb 16 '26 22:02

bvr