【问题标题】:How to read XML file into tabular format in SQL Server如何在 SQL Server 中将 XML 文件读取为表格格式
【发布时间】:2017-08-13 17:39:03
【问题描述】:

我通过不同的搜索发现了许多类似的帖子,其中给出了将 XML 转换为表格格式的解决方案。 下面是我附加的单行列的示例数据,也是我到目前为止所做的基本查询。

<DS_systeminfo>
<Systeminfo>
  <Property>CurrentLanguage</Property>
  <Property_value>en-US</Property_value>
</Systeminfo>
<Systeminfo>
  <Property>Manufacturer</Property>
  <Property_value>LENOVO</Property_value>
</Systeminfo>
<Systeminfo>
  <Property>SerialNumber</Property>
  <Property_value>789654</Property_value>
</Systeminfo>
<Systeminfo>
  <Property>Caption</Property>
  <Property_value>ATTT</Property_value>
</Systeminfo>   
  <Property>Manufacturer</Property>
  <Property_value>LENOVO</Property_value>
</Systeminfo>  
<Systeminfo>
  <Property>WindowsDirectory</Property>
  <Property_value>C:\WINDOWS</Property_value>
</Systeminfo>

query 如下:

SELECT SerialNumber, 
Cast(SystemInfoXML AS XML).value('(/DS_systeminfo/Systeminfo/Property)[1]', 'varchar(100)') AS Caption,
Cast(SystemInfoXML AS XML).value('(/DS_systeminfo/Systeminfo/Property_value)[1]', 'varchar(100)') AS Value
FROM TerminalsDetail

这只是获取第一个节点的值,我想在单个查询中动态选择所有节点,可能正在使用cursor

给出的数据是单行的,我有超过 100 行需要转换为表格格式。

任何好的建议都会有所帮助。

【问题讨论】:

  • 你永远不想在 SQL 中使用游标——永远。

标签: sql sql-server xml select transform


【解决方案1】:

类似这样的:

WITH the_data AS (
  SELECT CAST(SystemInfoXML AS XML) AS XML_DATA
  FROM TerminalsDetail
)
SELECT 
  XML_DATA.query('/DS_systeminfo/Systeminfo/Property[1]') as Caption,
  XML_DATA.query('/DS_systeminfo/Systeminfo/Property_value[1]') as Value
FROM cte

帽子提示

【讨论】:

    【解决方案2】:
    Declare @YourTable table (ID int,SystemInfoXML xml)
    Insert Into @YourTable values
    (1,'<DS_systeminfo><Systeminfo><Property>CurrentLanguage</Property><Property_value>en-US</Property_value></Systeminfo><Systeminfo><Property>Manufacturer</Property><Property_value>LENOVO</Property_value></Systeminfo><Systeminfo><Property>SerialNumber</Property><Property_value>789654</Property_value></Systeminfo><Systeminfo><Property>Caption</Property><Property_value>ATTT</Property_value></Systeminfo><Systeminfo><Property>Manufacturer</Property><Property_value>LENOVO</Property_value></Systeminfo><Systeminfo><Property>WindowsDirectory</Property><Property_value>C:\WINDOWS</Property_value></Systeminfo></DS_systeminfo>')
    
    Select A.ID
          ,B.*
     From  @YourTable A
     Cross Apply (
                    Select [Caption] = f.n.value('(Property)[1]','varchar(50)') 
                          ,[Value]   = f.n.value('(Property_value)[1]','varchar(50)') 
                     From  A.SystemInfoXML.nodes('DS_systeminfo') t(n)
                     Cross Apply t.n.nodes('Systeminfo') f(n)
                 ) B
    

    返回

    ID  Caption           Value
    1   CurrentLanguage   en-US
    1   Manufacturer      LENOVO
    1   SerialNumber      789654
    1   Caption           ATTT
    1   Manufacturer      LENOVO
    1   WindowsDirectory  C:\WINDOWS
    

    【讨论】:

      猜你喜欢
      • 2017-01-28
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多