Need to convert a SQL Server xml blob into a table for manipulation -


i have table has xml content stored string (this put before xml data types). here's example:

<?xml version="1.0" encoding="utf-16"?> <arrayoflibaddress xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema">   <libaddress address="11" type="user" />   <libaddress address="79" type="user" />   <libaddress address="18" type="usergroup" />   <libaddress address="19" type="usergroup" />   <libaddress address="5" type="mailgroup" />   <libaddress address="7" type="mailgroup" />   <libaddress address="someone@somewhere.com" type="emailaddress" />   <libaddress address="someoneelse@somewhere.com" type="emailaddress" /> </arrayoflibaddress> 

what need t-sql statement return following:

address                     type 11                          user 79                          user 18                          usergroup 19                          usergroup 5                           mailgroup 7                           mailgroup someone@somewhere.com       emailaddress someoneelse@somewhere.com   emailaddress 

for instance, if value loaded variable:

declare @x xml = '{the xml content above}' 

then how turn table?

i've found examples let me pull single value out element has attribute matches value, can't find example shows how turn attribute values of elements in document tabular result.

you can use nodes query on xml variable :

select   t.addresses.value('./@address', 'nvarchar(max)') [address],     t.addresses.value('./@type', 'nvarchar(max)') [type]   @x.nodes('//libaddress') t(addresses) 

see nodes() method on msdn more information


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -