Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. declare @xml xml
  2. set @xml = '<root>
  3. <group Description="firstgroup">
  4. <nodeA age="10" birthplace="Anchorage"/>
  5. <nodeB mode="A" ability="read"/>
  6. </group>
  7. <group Description="nextgroup">
  8. <nodeA age="10" birthplace="London"/>
  9. <nodeB count="2" birthplace="Paris"/>
  10. </group>
  11. </root>'
  12. select
  13. c.value('@Description', 'varchar(max)') as 'Description'
  14. from @xml.nodes('/root/*') as T(c)
  15.  
  16. select
  17. c.value('@Description', 'varchar(max)') as 'Description'
  18. , c.query('./nodeB') as Content
  19. from @xml.nodes('/root/*') as T(c)
  20.  
  21. -- Results to:
  22. Description Content
  23. firstgroup <nodeB mode="A" ability="read" />
  24. nextgroup <nodeB count="2" birthplace="Paris" />
  25.  
  26. Select c.value('@Description', 'varchar(max)') as 'Description'
  27. ,AsString = convert(varchar(max),c.query('./*[2]') )
  28. ,AsXML = c.query('./*[2]')
  29. From @xml.nodes('/root/*') as T(c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement