【问题标题】:select distinct values on XMLType在 XMLType 上选择不同的值
【发布时间】:2010-11-07 00:35:01
【问题描述】:

我有一个包含 XMLType 字段的表。该表是使用以下 DDL/DML 创建和加载的:

CREATE TABLE T_ECO_test_LOG
(
  SECID             NUMBER                      NOT NULL,
  LOG_ATTRIBUTES    SYS.XMLTYPE
)

INSERT INTO t_eco_test_log VALUES 
   (       1, XMLType(
              '<attributes>
  <attribute>
    <name>remoteAddress</name>
    <value>180.201.106.130</value>
  </attribute>
  <attribute>
    <name>domain</name>
    <value>BSI_US</value>
  </attribute>
</attributes>'));

INSERT INTO t_eco_test_log VALUES 
   (       2, XMLType(
              '<attributes>
  <attribute>
    <name>user</name>
    <value>xxxx</value>
  </attribute>
  <attribute>
    <name>domain</name>
    <value>BSI_US</value>
  </attribute>
</attributes>'));        

我想逐行获取/attributes/attribute/name 中的不同值;所以O想得到的数据:

remoteAddress
domain
user

到目前为止,我已经尝试了以下查询:

select extractValue(value(x),'/attributes/attribute/name') 
  from t_eco_log,
        table(xmlsequence(extract(log_attributes,'/attributes')) )x

但我收到以下消息:

ORA-19025:EXTRACTVALUE 仅返回一个节点的值

如果我使用

select extract(value(x),'/attributes/attribute/name') 
  from t_eco_log,
        table(xmlsequence(extract(log_attributes,'/attributes')) )x

我得到了一个 XML 结果,其中包含:

<name>remoteAddress</name><name>domain</name>

但我想将它们作为行获取,我该怎么做?

TIA

【问题讨论】:

    标签: sql oracle select xmltype extract-value


    【解决方案1】:

    类似:

    with x1 as (select xmltype('<attributes>
      <attribute>
        <name>remoteAddress</name>
        <value>180.201.106.130</value>
      </attribute>
      <attribute>
        <name>domain</name>
        <value>BSI_US</value>
      </attribute>
    </attributes>') x2 from dual)
    select extract(value(x3),'/attribute/name') 
      from x1,
            table(xmlsequence(extract(x2,'/attributes/*')) ) x3
    

    如果你提供 CREATE TABLE 和 INSERT,那么更容易给出精确的 SQL

    【讨论】:

    • 我已经编辑了这个问题来澄清它,给出了一个表格和数据的例子,以及我想要得到什么。感谢您的帮助!
    【解决方案2】:

    我明白了。根据 Gary 的说法:

    with x1 as (select log_attributes x2 from t_eco_test_log)
    select distinct(extractValue(value(x3),'/attribute/name')) 
      from x1,
            table(xmlsequence(extract(x2,'/attributes/*')) ) x3
    

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 2020-02-19
      • 2010-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多