I have the following XML data in a file "Test1.xml":
<TextValuess>
<TextValues Name="Value1" Override="true" Type="String">
<DEV>Source=DEV;Catalog=DEV_DMT;Integrated Security=SSPI;</DEV>
<INT10>Source=LAB;Catalog=TST_INT10;Integrated Security=SSPI;</INT10>
<INT>Source=LAB1;Catalog=TST_INT1;Integrated Security=SSPI;</INT>
<INT2>Source=LAB10;Catalog=TST_INT12;Integrated Security=SSPI;</INT2>
</TextValues>
<TextValues Name="ENVIRONMENT" Override="true" Type="String">
<DEV>DEV</DEV>
<INT10>INT10</INT10>
<INT>INT1</INT>
<INT2>INT15</INT2>
</TextValues>
</TextValuess>
I am trying to read the INT10 values and Name of the <TextValues> tag. I need output like below in SQL Server:
Name Value
---- -----
Value1 LAB
Environment INT10
I have tried with these SQL statements. I was able to get either the Name value or the INT10 values.
Statement 1:
select c3.value('INT10[1]','varchar(50)')
from
(select cast(c1 as xml)
from OPENROWSET (BULK 'D:\Tasks\Test1.xml',SINGLE_BLOB) as T1(c1)) as T2(c2)
cross apply c2.nodes('/TextValuess/TextValues') T3(c3)
With this I was able to retrieve the values for INT10
Statement 2:
DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)
SELECT @XML = ' <TextValuess>
<TextValues Name="Value1" Override="true" Type="String">
<DEV>Source=DEV;Catalog=DEV_DMT;Integrated Security=SSPI;</DEV>
<INT10>Source=LAB;Catalog=TST_INT10;Integrated Security=SSPI;</INT10>
<INT>Source=LAB1;Catalog=TST_INT1;Integrated Security=SSPI;</INT>
<INT2>Source=LAB10;Catalog=TST_INT12;Integrated Security=SSPI;</INT2>
</TextValues>
<TextValues Name="ENVIRONMENT" Override="true" Type="String">
<DEV>DEV</DEV>
<INT10>INT10</INT10>
<INT>INT1</INT>
<INT2>INT15</INT2>
</TextValues>
</TextValuess>'
EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML
SELECT Name ,INT10
FROM OPENXML(@hDoc, 'TextValuess/TextValues/INT10')
WITH
(
Name [varchar](50) '../@Name',
INT10 [varchar](100) '../@INT10'
)
With this I was able to retrieve only Name Information but not the INT10 Value.