2011-10-27 88 views
2

由於SQL ...轉換SQL XML數據類型表

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>' 

會是什麼SQL到XML轉換成兩列是PersonID和姓氏的表?

回答

2
SELECT T.c.query('.').value('(//@PersonID)[1]', 'int'), 
    T.c.query('.').value('(//@LastName)[1]', 'varchar(50)') 
FROM @xmlDoc.nodes('/people/person') T(c) 
+0

感謝您的幫助! –

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)