-2

我想阅读一个 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
4

1 回答 1

0

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

您可以使用cross apply将此操作扩展到所有<entry>标签

这是一个可以用作起点的基本查询:

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)

结果:

在此处输入图像描述

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

于 2020-08-11T13:18:16.100 回答