【问题标题】:Deleting Row if only <Node 1> exists, Delete only <Node 1> if <Node 2> <Node 3> exsist如果只有 <Node 1> 存在则删除行,如果 <Node 2> <Node 3> 存在则仅删除 <Node 1>
【发布时间】:2019-01-12 03:01:55
【问题描述】:

考虑下面有 2 行的表格。

ID_NUM      | GENERAL_DATA
0100        | <...><UpdateServices><AddServices><RemoveServices><...>
0200        | <...><UpdateServices><...>

我的问题是:

如果 XML 仅具有节点 &lt;UpdateServices&gt;(意味着 &lt;AddServices&gt;&lt;RemoveServices&gt; 不存在),如何删除该行?和

如果存在&lt;AddServices&gt;&lt;RemoveServices&gt;,如何从XML 中删除节点&lt;UpdateServices&gt;

CREATE TABLE SAMPLE_TABLE AS
  select '0100' as id_num,
  XMLTYPE('<TRX>
  <DATA>
      <Request APIType="null">
          <SubscriberIdsInfo>
              <ExternalId>
                  <ExternalId>0100</ExternalId>
              </ExternalId>
              <SubscriberId>
                  <SubscrNumber/>
              </SubscriberId>
          </SubscriberIdsInfo>
          <UpdateServices>
              <Soc>ABC</Soc>
              <ServiceAgreementSequenceNo/>
              <DealerCode/>
              <DeployMode/>
              <EffectiveDate>2019-10-16T00:00:00</EffectiveDate>
              <ExpirationDate/>
              <OfferInstanceId/>
          </UpdateServices>
          <AddServices>
              <Soc>ABC1</Soc>
              <ServiceAgreementSequenceNo/>
              <DealerCode/>
              <DeployMode/>
              <EffectiveDate>2018-10-16T00:00:00</EffectiveDate>
              <ExpirationDate/>
              <OfferInstanceId/>
          </AddServices>
          <RemoveServices>
              <Soc>ABC2</Soc>
              <ServiceAgreementSequenceNo/>
              <DealerCode/>
              <DeployMode/>
              <EffectiveDate>2017-10-16T00:00:00</EffectiveDate>
              <ExpirationDate/>
              <OfferInstanceId/>
          </RemoveServices>
          <SubParameters>
              <Name>PoolID</Name>
              <Values>POOL0100</Values>
              <EffectiveDate>2014-10-16T14:08:37</EffectiveDate>
              <ExpirationDate/>
          </SubParameters>
          <ActivityInfo/>
      </Request>
  </DATA>
  </TRX>') general_data from dual;


 insert into SAMPLE_TABLE (id_num,general_data)
  select  '0200' as id_num, 
  XMLTYPE('<TRX>
  <DATA>
      <Request APIType="null">
          <SubscriberIdsInfo>
              <ExternalId>
                  <ExternalId>0200</ExternalId>
              </ExternalId>
              <SubscriberId>
                  <SubscrNumber/>
              </SubscriberId>
          </SubscriberIdsInfo>
          <UpdateServices>
              <Soc>ABC</Soc>
              <ServiceAgreementSequenceNo/>
              <DealerCode/>
              <DeployMode/>
              <EffectiveDate>2019-10-16T00:00:00</EffectiveDate>
              <ExpirationDate/>
              <OfferInstanceId/>
          </UpdateServices>
          <SubParameters>
              <Name>PoolID</Name>
              <Values>POOL0200</Values>
              <EffectiveDate>2014-10-16T14:08:37</EffectiveDate>
              <ExpirationDate/>
          </SubParameters>
          <ActivityInfo/>
      </Request>
  </DATA>
  </TRX>') general_data from dual;


commit;

谢谢。 :)

【问题讨论】:

    标签: sql xml plsql


    【解决方案1】:

    如果 XML 仅有节点 &lt;UpdateServices&gt;(即不存在 &lt;AddServices&gt;&lt;RemoveServices&gt;),如何删除行

    delete from sample_table X
    where xmlExists('//UpdateServices' passing X.general_data)
        and not xmlExists('//AddServices|//RemoveServices' passing X.general_data)
    ;
    

    如果存在&lt;AddServices&gt;&lt;RemoveServices&gt;,如何从XML 中删除节点&lt;UpdateServices&gt;

    update sample_table X
    set X.general_data = deleteXml(X.general_data, '//UpdateServices')
    where xmlExists('//AddServices' passing X.general_data)
        and xmlExists('//RemoveServices' passing X.general_data)
    ;
    

    注意:我相信后者可以通过 XQuery 更有效地完成,但由于我(仍然)不懂那种语言,所以我只能提供 deleteXml() 解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 2014-12-24
      • 2016-07-08
      相关资源
      最近更新 更多