SUM和COUNT xPath表达式在Oracle 11.2中不起作用

时间:2014-04-24 11:07:07

标签: sql xml oracle xpath plsql

运行一些 - 相当简单 - 在Oracle(11g R2)中包含SUMCOUNT函数的xPath查询时遇到问题。例如:

select extractvalue(
                    xmltype.createxml('<a>
                                         <b>
                                           <c>1</c>
                                           <d>XXX</d>
                                         </b>
                                         <b>
                                           <c>2</c>
                                           <d>YYY</d>
                                         </b>
                                       </a>')
                    , 'sum(/a/b/c)'
                    ) 
  from dual;

count

select extractvalue(
                    xmltype.createxml('<a>
                                         <b>
                                           <c>1</c>
                                           <d>XXX</d>
                                         </b>
                                         <b>
                                           <c>2</c>
                                           <d>YYY</d>
                                         </b>
                                      </a>')
                   , 'count(/a/b/c)'
                   ) 
   from dual;

我得到ORA-31012作为回应。知道如何让它工作吗?或者可能的解决方法是什么?

2 个答案:

答案 0 :(得分:2)

我不记得我最后一次使用extractvalue因为它在11g中被剔除了。相反,我使用xmlquery

以下是countsum的简单示例:

with 
xmldata as (
  select xmltype('<a><b><c>1</c><d>TEXT1</d></b><b><c>2</c><d>TEXT2</d></b></a>') as data_ from dual
)
select 'COUNT', xmlquery('
count($doc/a/b/c)
' passing data_ as "doc" returning content) as result_
from xmldata
union all
select 'SUM', xmlquery('
sum($doc/a/b/c)
' passing data_ as "doc" returning content) as result_
from xmldata
;

希望这有帮助!

答案 1 :(得分:2)

我尝试了@ user272735的回答而没有使用as "doc"并删除了$doc,它似乎也是一样的:

with 
xmldata as (
  select xmltype('<a><b><c>1</c><d>TEXT1</d></b><b><c>2</c><d>TEXT2</d></b></a>') as data_ from dual
)
select 'COUNT', xmlquery('
count(/a/b/c)
' passing data_ returning content) a as s result_
from xmldata
union all
select 'SUM', xmlquery('
sum(/a/b/c)
' passing data_ returning content) as result_
from xmldata
;