【问题标题】:Read key Value pair XML from nvarchar in query which returns value in columns在查询中从 nvarchar 读取键值对 XML,该查询返回列中的值
【发布时间】:2020-11-24 22:29:27
【问题描述】:

我想读取一个 XML nvarchar 列:

<Attributes>
  <Map>
    <entry key="costcenter">
      <value>
        <List>
          <String>R03e</String>
          <String>L07e</String>
        </List>
      </value>
    </entry>
    <entry key="department" value="Information Technology"/>
    <entry key="email" value="Paul.Walker@demoexample.com"/>
    <entry key="employeeId" value="1a2a3b4d"/>
    <entry key="firstName" value="Paul"/>
    <entry key="fullName" value="Paul.Walker"/>
    <entry key="inactiveIdentity" value="FALSE"/>
    <entry key="lastName" value="Walker"/>
    <entry key="location" value="Austin"/>
    <entry key="managerId" value="1a2c3a4d"/>
    <entry key="region" value="Americas"/>
  </Map>
</Attributes>

我想要这样的列中的结果:

costSenter | department | email | employeeId | firstName | lastName | location | managerId | region

【问题讨论】:

  • 检查这是否可以帮助你stackoverflow.com/questions/23096527/…
  • "clob" 是 oracle 数据类型,对吗?如果是,为什么要标记 SQL Server?
  • 你尝试过什么,你在哪里卡住了?我们需要看看你的尝试......
  • @SM或者是它的 nvarchar(max) 。为了一般理解,我说它是clob。
  • @DaleK 我尝试了我能在这里找到的任何东西。但计数找到我拥有的任何用于 XML 格式的解析器。

标签: sql sql-server xml tsql parsexml


【解决方案1】:

您可以使用nodes xml 方法(更多信息here)来粉碎您的xml,应用属性过滤并从value 属性中提取信息。

您可以使用cross apply将此操作扩展到所有&lt;entry&gt;标签

这是一个基本查询,您可以将其用作起点:

declare @src nvarchar(max)='
<Attributes>
  <Map>
    <entry key="costcenter">
      <value>
        <List>
          <String>R03e</String>
          <String>L07e</String>
        </List>
      </value>
    </entry>
    <entry key="department" value="Information Technology"/>
    <entry key="email" value="Paul.Walker@demoexample.com"/>
    <entry key="employeeId" value="1a2a3b4d"/>
    <entry key="firstName" value="Paul"/>
    <entry key="fullName" value="Paul.Walker"/>
    <entry key="inactiveIdentity" value="FALSE"/>
    <entry key="lastName" value="Walker"/>
    <entry key="location" value="Austin"/>
    <entry key="managerId" value="1a2c3a4d"/>
    <entry key="region" value="Americas"/>
  </Map>
</Attributes>'

;with x as
(
    select  cast(@src as xml) as attributes
)
select 
      costcenter.s.value('.', 'nvarchar(max)') as costcenter
    , department.s.value('.', 'nvarchar(max)') as department
    , email.s.value('.', 'nvarchar(max)') as email
    , employeeId.s.value('.', 'nvarchar(max)') as employeeId
    , firstName.s.value('.', 'nvarchar(max)') as firstName
    , fullName.s.value('.', 'nvarchar(max)') as fullName
    , inactiveIdentity.s.value('.', 'nvarchar(max)') as inactiveIdentity
    , lastName.s.value('.', 'nvarchar(max)') as lastName
    , loc.s.value('.', 'nvarchar(max)') as [location]
    , managerID.s.value('.', 'nvarchar(max)') as managerId
    , region.s.value('.', 'nvarchar(max)') as region
 
from x
    CROSS APPLY attributes.nodes('//entry[@key = "costcenter"]/value') costcenter(s)
    CROSS APPLY attributes.nodes('//entry[@key = "department"]/@value') department(s)
    CROSS APPLY attributes.nodes('//entry[@key = "email"]/@value') email(s)
    CROSS APPLY attributes.nodes('//entry[@key = "employeeId"]/@value') employeeId(s)
    CROSS APPLY attributes.nodes('//entry[@key = "firstName"]/@value') firstName(s)
    CROSS APPLY attributes.nodes('//entry[@key = "fullName"]/@value') fullName(s)
    CROSS APPLY attributes.nodes('//entry[@key = "inactiveIdentity"]/@value') inactiveIdentity(s)
    CROSS APPLY attributes.nodes('//entry[@key = "lastName"]/@value') lastName(s)
    CROSS APPLY attributes.nodes('//entry[@key = "location"]/@value') loc(s)
    CROSS APPLY attributes.nodes('//entry[@key = "managerId"]/@value') managerID(s)
    CROSS APPLY attributes.nodes('//entry[@key = "region"]/@value') region(s)

结果:

附注以后请遵循您在 cmets 中获得的建议:发布您迄今为止尝试过的代码,突出显示您无法解决的问题。

【讨论】:

    猜你喜欢
    • 2021-06-29
    • 2011-12-11
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多