2

Given the SQL...

declare @xmlDoc xml
set @xmlDoc = '<people>
  <person PersonID="8" LastName="asdf" />
  <person PersonID="26" LastName="rtert" />
  <person PersonID="33" LastName="dfgh" />
  <person PersonID="514" LastName="ukyy" />
</people>'

What would be the sql to convert that xml into a table of two columns PersonID and LastName?

John Livermore
  • 30,235
  • 44
  • 126
  • 216

2 Answers2

2
SELECT T.c.query('.').value('(//@PersonID)[1]', 'int'),
    T.c.query('.').value('(//@LastName)[1]', 'varchar(50)')
FROM @xmlDoc.nodes('/people/person') T(c)
Stuart Ainsworth
  • 12,792
  • 41
  • 46
2
select T.X.value('@PersonID', 'int') as PersonID,
       T.X.value('@LastName', 'nvarchar(50)') as LastName
from @xmlDoc.nodes('/people/person') as T(X)
Mikael Eriksson
  • 136,425
  • 22
  • 210
  • 281