【问题标题】:Need to fetch XML data from XML column to other column需要从 XML 列获取 XML 数据到其他列
【发布时间】:2021-03-21 16:52:43
【问题描述】:

我有一张如下表。我有一个 xml 列,我需要从 XML 标记中获取记录并存储在一个新列中。

Declare @t table (ID Int, xmldata xml)
Insert into @t values (1,'<C>
  <Dir xmlns="$Recycle.Bin" />
  <Dir xmlns="$SysReset" />
  <Dir xmlns="AppData" />
  <File>bootmgr<Size>375</Size></File>
  <File>BOOTNXT<Size>1</Size></File>
  <Dir xmlns="Config.Msi" />
  <Dir xmlns="Documents and Settings" />
  <File>Edge.log<Size>11</Size></File>
  <File>hiberfil.sys<Size>815856</Size></File>
  <Dir xmlns="Intel" />
  <Dir xmlns="MSOCache" />
  <Dir xmlns="PerfLogs" />
  <File>port.txt<Size>1</Size></File>
  <Dir xmlns="Program Files" />
  <Dir xmlns="Program Files (x86)" />
  <Dir xmlns="ProgramData" />
  <Dir xmlns="Recovery" />
  <File>rescuepe.log<Size>1</Size></File>
  <Dir xmlns="System Volume Information" />
  <File>TestAddException.txt<Size>1</Size></File>
  <Dir xmlns="Users" />
  <Dir xmlns="Windows" />
  <Dir xmlns="XLM" />
</C>')

Insert into @t values (2,'<C>
  <Dir xmlns="$RECYCLE.BIN" />
  <Dir xmlns="$SysReset" />
  <File>bootmgr<Size>375</Size></File>
  <File>BOOTNXT<Size>1</Size></File>
  <Dir xmlns="Config.Msi" />
  <Dir xmlns="Documents and Settings" />
  <File>Edge.log<Size>11</Size></File>
  <File>hiberfil.sys<Size>815856</Size></File>
  <Dir xmlns="Intel" />
  <Dir xmlns="MSOCache" />
  <Dir xmlns="PerfLogs" />
  <File>port.txt<Size>1</Size></File>
  <Dir xmlns="Program Files" />
  <Dir xmlns="Program Files (x86)" />
  <Dir xmlns="ProgramData" />
  <Dir xmlns="Recovery" />
  <File>rescuepe.log<Size>1</Size></File>
  <Dir xmlns="System Volume Information" />
  <File>TestAddException.txt<Size>1</Size></File>
  <Dir xmlns="Users" />
  <Dir xmlns="Windows" />
  <Dir xmlns="XLM" />
</C>')

预期输出。

ID      FileName            Size        
1       bootmgr             375 
1       BOOTNXT             1   
1       Edge                11  
1       hiberfil            815856  
1       port                1   
1       rescuepe            1   
1       TestAddException    1   
2       bootmgr             375 
2       BOOTNXT             1   
2       Edge                11  
2       hiberfil            815856  
2       port                1   
2       rescuepe            1   
2       TestAddException    1   

我试过This link

但它没有用。我以前从未尝试过 XML,有人可以帮忙。

【问题讨论】:

  • 投反对票的原因??

标签: sql sql-server sql-server-2008


【解决方案1】:
select t.ID, 
    c.f.value('text()[1]', 'nvarchar(200)') as filename,
    c.f.value('(Size/text())[1]', 'varchar(50)') as size
from @t as t
cross apply t.xmldata.nodes('C/File') as c(f);

【讨论】:

  • 太棒了!太感谢了。我为此苦苦挣扎了很久。
猜你喜欢
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-21
  • 2018-09-01
  • 2012-03-11
  • 1970-01-01
相关资源
最近更新 更多