Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Como ler a coluna XML no SQL Server 2008?

with xmlnamespaces('http://schemas.microsoft.com/office/infopath/2003/myXSD/2014-03-29T09:41:23' as my)
select M.XMLData.value('(/my:myFields/my:field1/text())[1]', 'int') as field1,
       M.XMLData.value('(/my:myFields/my:field2/text())[1]', 'int') as field2,
       M.XMLData.value('(/my:myFields/my:field3/text())[1]', 'bit') as field3,
       M.XMLData.value('(/my:myFields/my:FormName/text())[1]', 'datetime') as FormName,
       (
         select ','+R.X.value('text()[1]', 'nvarchar(max)')
         from M.XMLData.nodes('/my:myFields/my:Repeating') as R(X)
         for xml path(''), type
       ).value('substring(text()[1], 2)', 'nvarchar(max)') as Repeating
from XMLMain as M

Resultado:
field1      field2      field3 FormName                Repeating
----------- ----------- ------ ----------------------- -----------------------
1           2           1      2014-04-01 15:11:47.000 hi,hello,how are  you?