Save XML with attribute to Table in SQL Server -


hi have xml data attribute input sql, need inserted in table. xml data is

<?xml version="1.0" encoding="iso-8859-1"?> <messageack>  <guid guid="kfafb30" submitdate="2015-10-15 11:30:29" id="1"> <error seq="1" code="28681" /> </guid>  <guid guid="kfafb3" submitdate="2015-10-15 11:30:29" id="1"> <error seq="2" code="286381" /> </guid>  </messageack> 

i want inserted in below format

guid         submit date          id          error seq     code kfafb3   2015-10-15 11:30:29      1          1             28681 kfafb3   2015-10-15 11:30:29      1          1             2868 

please help.

look xpath , xml data type methods in msdn. 1 possible way :

declare @xml xml = '...you xml string here...' insert yourtable select      guid.value('@guid', 'varchar(100)') 'guid'     ,guid.value('@submitdate', 'datetime') 'submit date'     ,guid.value('@id', 'int') 'id'     ,guid.value('error[1]/@seq', 'int') 'seq'     ,guid.value('error[1]/@code', 'int') 'code' @xml.nodes('/messageack/guid') x(guid) 

result :

enter image description here


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -