Append Xml Attributes in a SQL server Query -


i'm new in handling xml inside sql server , have question have not found example or answer, useful if guys me.

i have various xml iside table "t1" under xml column type "xmlcol", xml structure:

row 1 <icfd:ips totalir="0" totalit="7223.9">   <icfd:tras>     <icfd:tra impt="aiv" ts="16" imp="517.1"/>     <icfd:tra impt="sri" ts="8" imp="18.3"/>   </icfd:tras> </icfd:ips>  row 2 <icfd:ips totalir="10" totalit="123.9">   <icfd:tras>     <icfd:tra impt="aiv" ts="34" imp="345.1"/>   </icfd:tras> </icfd:ips> 

i'm trying obtain result this:

tra                     totalir    totalit ------------------------------------------- aiv(517.1),sri(18.3)    0          7223.9 aiv(345.1)              10         123.9 

how correct way make query?

i have i'm getting nowhere:

------------------ sql server query -----------------------       xmlnamespaces ('http://www.stwaree/icfd/3' icfd) select      b.value('@impt', 'varchar(max)') + '(' +     b.value('@imp', 'varchar(max)') + ')' tra     [t1] cross apply [xmlcol].nodes('//icfd:tra') a(b)  ----------------------- result ----------------------------  tra         ---------- aiv(517.1) sri(18.3) aiv(345.1) 

thanks help.

with xmlnamespaces('http://www.stwaree/icfd/3' icfd) select t.xmlcol.query('for $i in /icfd:ips/icfd:tras/icfd:tra                         return element x{concat(",", data($i/@impt), "(", data($i/@imp), ")")}'                      ).value('substring(., 2)', 'nvarchar(max)') tra,        t.xmlcol.value('/icfd:ips[1]/@totalir', 'varchar(10)') totalir,        t.xmlcol.value('/icfd:ips[1]/@totalit', 'varchar(10)') totalit t; 

sql fiddle


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -