SQL server xquery

一曲冷凌霜 提交于 2019-12-24 00:52:09

问题


I have a xml data

<Education>

          <School ID="1">
          <Degree>test</Degree>
          <Description>desc1</Description>
          <Institution>kec</Institution>
          </School>

          <School ID="2">
          <Degree>test2</Degree>
          <Description>desc2</Description>
          <Institution>kec2</Institution>
             </School>

      </Education>

now I need to extract the School detail into some temp table

like

----------------------------------
schoolid degree desc instituion
----------------------------------
1        test    desc1  kec
2        test2   desc2  kec2

can anyone please help me


回答1:


Use:

declare @x xml='<Education>

          <School ID="1">
          <Degree>test</Degree>
          <Description>desc1</Description>
          <Institution>kec</Institution>
          </School>

          <School ID="2">
          <Degree>test2</Degree>
          <Description>desc2</Description>
          <Institution>kec2</Institution>
             </School>

      </Education>'


select t.s.value('@ID', 'int') [schoolid]
    , t.s.value('Degree[1]', 'nvarchar(20)') [degree]
    , t.s.value('Description[1]', 'nvarchar(20)') [desc]
    , t.s.value('Institution[1]', 'nvarchar(20)') [instituion]
from @x.nodes('Education/School') t(s)

Output:

schoolid    degree               desc                 instituion
----------- -------------------- -------------------- --------------------
1           test                 desc1                kec
2           test2                desc2                kec2


来源:https://stackoverflow.com/questions/8951552/sql-server-xquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!