i want to convert records in a table into xml in sql server as shown below -


i want convert records in table xml in sql server shown below.

table

countryid   stateid siteid  value 1   1   1   11 1   1   2   22 1   2   1   55 1   2   2   66 2   1   1   111 2   1   2   222 2   2   1   555 2   2   2   666 3   1   1   100 3   1   2   200 3   2   1   500 3   2   2   600 

i want convert data xml shown below.

<data>       <countrydata>          <countryid> 1 </countryid>          <stateid> 1 </stateid>         <siteid> 1 </siteid>         <value> 11 </value>     </countrydata>     <countrydata>          <countryid> 1 </countryid>          <stateid> 1 </stateid>         <siteid> 2 </siteid>         <value> 22 </value>     </countrydata>     <countrydata>          <countryid> 1 </countryid>          <stateid> 2 </stateid>         <siteid> 1 </siteid>         <value> 55 </value>     </countrydata>     <countrydata>          <countryid> 1 </countryid>          <stateid> 2 </stateid>         <siteid> 2 </siteid>         <value> 66 </value>     </countrydata> </data> 

this xml expect records countryid = 1..

similarly need different xml records different countryids.

have @ for xml (sql server)

a select query returns results rowset. can optionally retrieve formal results of sql query xml specifying xml clause in query.

also, through examples found @ use raw mode xml , more at

how like

select [countryid], [stateid], [siteid], [value] table1 xml raw ('countrydata'), elements, root ('data') 

sql fiddle demo


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -