I have the following XML data and would like to get data inside KS as row as follows:
<DW>
<KS>
<KeyInfo Name="IlluSetting">
<KeyTypeValue>Text</KeyTypeValue>
<ValueString>yDipol90</ValueString>
</KeyInfo>
<KeyInfo Name="IlluSetting2">
<KeyTypeValue>Text</KeyTypeValue>
<ValueString>yDipol</ValueString>
</KeyInfo>
</KS>
<MDESC>Tx [mrad]</MDESC>
<MNUMBER>0.12102</MNUMBER>
</DW>
<DW>
<KS>
<KeyInfo Name="IlluSetting3">
<KeyTypeValue>Text</KeyTypeValue>
<ValueString>yDipol80</ValueString>
</KeyInfo>
</KS>
<MDESC>Ty [mrad]</MDESC>
<MNUMBER>0.12102</MNUMBER>
</DW>
Is there any way to get a Table with the following output:
Name ValueString Name ValueString
-----------------------------------------------------------
IlluSetting yDipol90 IlluSetting2 yDipol
IlluSetting3 yDipol80
which means that the data inside <KS>... </KS> will be shown in a row
Many thanks
Please try the following solution. What we are doing here is called shredding, i.e. converting XML into rectangular/relational format.
I am shooting from the hip because DDL and sample data population were not provided.
The provided XML is not well-formed due to missing root element, but SQL Server allows to handle XML fragments.
We are using XQuery and its
.nodes()and.value()methods.SQL, Method #1
Output
SQL, Method #2