How to Extract data from xml column in sql 2008

回眸只為那壹抹淺笑 提交于 2019-12-01 12:39:21

问题


I have a table with a column of type xml. I have to extract the values from the xml column. I have tried all possible ways and i couldn succeed.Please find below the sample of the xml data.

<Menu>
  <Id>1</Id>
    <Url>http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-1.jpg</Url>
</Menu>
<Menu>
  <Id>2</Id>
  <Url>http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-2.jpg</Url>
</Menu>
<Menu>
  <Id>3</Id>
  <Url>http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-3.jpg</Url>
</Menu>
<Menu>
  <Id>4</Id>
  <Url>http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-4.jpg</Url>
</Menu>
<Menu>
  <Id>5</Id>
  <Url>http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-5.jpg</Url>
</Menu>

Please help me to extract the data. Tons of thanks in advance.

Thanks, Darthick. s.darthick@gmail.com


回答1:


declare @T table
(
  XMLCol xml
)

insert into @T values
('<Menu>
    <Id>1</Id>
      <Url>http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-1.jpg</Url>
  </Menu>
  <Menu>
    <Id>2</Id>
    <Url>http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-2.jpg</Url>
  </Menu>
  <Menu>
    <Id>3</Id>
    <Url>http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-3.jpg</Url>
  </Menu>
  <Menu>
    <Id>4</Id>
    <Url>http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-4.jpg</Url>
  </Menu>
  <Menu>
    <Id>5</Id>
    <Url>http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-5.jpg</Url>
  </Menu>')

select X.N.value('Id[1]', 'int') as Id,
       X.N.value('Url[1]', 'varchar(max)') as Url
from @T as T
  cross apply T.XMLCol.nodes('/Menu') as X(N)

Result:

Id          Url
----------- ---------------------------------------------------------------------
1           http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-1.jpg
2           http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-2.jpg
3           http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-3.jpg
4           http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-4.jpg
5           http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-5.jpg



回答2:


If there is namespace. XML:

<PersonInfo xmlns="http://mynamspace.lv/default">
    <Person ID="22">
        <Name>MyName</Name>
        <Profession>MyProfession</Profession>
    </Person>
</PersonInfo>

Then you should do something like this:

WITH XMLNAMESPACES (DEFAULT 'http://mynamspace.lv/default')
SELECT
    @myXML.value('(PersonInfo/Person/@ID)[1]', 'nvarchar(100)') as ID,
    @myXML.value('(PersonInfo/Person/Name)[1]', 'nvarchar(100)') as Name,
    @myXML.value('(PersonInfo/Person/Profession)[1]', 'nvarchar(100)') as Profession;


来源:https://stackoverflow.com/questions/9600361/how-to-extract-data-from-xml-column-in-sql-2008

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