How to write a xquery in SQL Server to read all the childnodes from an XML data column in a table? -
i have table in sql server named [queryresult] columns of type init, of type xml.
here sample xml of result column data.
<output> <item> <value001>2023038.09</value001> <value002>265127.97</value002> <value003>459868.86</value003> <value004>16000.01</value004> <value005>552010.15</value005> <value006>21404.96</value006> <item> <item> <value001>15655501.89</value001> <value002>17172.50</value002> <value003>8345.31</value003> <value004>-117264374.12</value004> <value005>-160893.48</value005> </item> </output>
i need query returns output follows:
id element value ----------------------------------- 1 value001 2023038.09 2 value001 2023038.09 3 value002 265127.97 4 value003 459868.86 5 value004 16000.01 6 value005 552010.15 7 value006 21404.96 8 value001 15655501.89 9 value002 17172.50 10 value003 8345.31 11 value004 -117264374.12 12 value005 -160893.48
let me know if question not clear you?
here's 1 solution:
declare @xml xml = ' <output> <item> <value001>2023038.09</value001> <value002>265127.97</value002> <value003>459868.86</value003> <value004>16000.01</value004> <value005>552010.15</value005> <value006>21404.96</value006> </item> <item> <value001>15655501.89</value001> <value002>17172.50</value002> <value003>8345.31</value003> <value004>-117264374.12</value004> <value005>-160893.48</value005> </item> </output> ' select t.c.value('local-name(.)[1]', 'varchar(100)') element, t.c.value('./text()[1]', 'decimal(17,2)') value @xml.nodes('//item/child::node()') t(c);
returning ordinal position of node more difficult looks. this question suggests 1 solution haven't implemented here it's bit of hack.
Comments
Post a Comment