TSQL如果其子元素包含给定值,则删除XML节点

时间:2014-01-01 00:46:07

标签: xml tsql

XML存储可以在<Address>下拥有零个或多个<Addresses>个节点 需要查询才能删除<Address>

的所有<AddressLine> = '2 Church Lane'个节点

启动XML:

declare @t table (storage xml);
insert into @t (storage)
select '<Person>
  <Fullname />
  <Surname>Smith</Surname>
  <FirstName>John</FirstName>
  <Addresses>
      <Address>
        <AddressLine>1 Church Lane</AddressLine>
      </Address>
      <Address>
        <AddressLine>2 Church Lane</AddressLine>
      </Address>
      <Address>
        <AddressLine>3 Church Lane</AddressLine>
      </Address>
  </Addresses>
</Person>';

产生的XML:

<Person>
  <Fullname />
  <Surname>Smith</Surname>
  <FirstName>John</FirstName>
  <Addresses>
      <Address>
        <AddressLine>1 Church Lane</AddressLine>
      </Address>
      <Address>
        <AddressLine>3 Church Lane</AddressLine>
      </Address>
  </Addresses>
</Person>

1 个答案:

答案 0 :(得分:5)

找到它:

update @t
SET storage.modify('
    delete (/Person/Addresses/Address[AddressLine = "2 Church Lane"])
')