Question

When I run an XQuery query in PL-SQL using

 SELECT XQUERY('...' RETURNING CONTENT) FROM DUAL

the result is always returned concatenated in a single row single column holding a SQLXML field.

This is bad for queries returning sequences of elements and really nasty for queries returning sequences of text nodes.

Is there a way to avoid this concatenation and get a row per returned item?

Thanks,

Eric

Was it helpful?

Solution 2

To elaborate on jonearles' answer, XMLTable is as generic as XQUERY, the difference being that XQUERY returns a single value while XMLTable is seen as full table.

With XMLTable, you can do things such as:

SQL> select * from XMLTABLE ('declare variable $v as xs:string external; $v, $v' PASSING 'Hello World' as "v") ;

COLUMN_VALUE
--------------------------------------------------------------------------------
Hello World
Hello World

and also:

SQL> select * from XMLTABLE ('declare variable $v as xs:string external; <e>{$v}</e>,<e>{$v}</e>' PASSING 'Hello World' as "v") ;

COLUMN_VALUE
--------------------------------------------------------------------------------   
<e>Hello World</e>
<e>Hello World</e>

and that does exactly what I was looking for.

OTHER TIPS

You want to use XMLTable instead of XQuery.

XMLTable maps the result of an XQuery evaluation into relational rows and columns. You can query the result returned by the function as a virtual relational table using SQL.

For example:

create table customer as
select 1 id, 'Smith'   last_name from dual union all
select 2 id, 'Jackson' last_name from dual union all
select 3 id, 'Peters'  last_name from dual;

SELECT * from XMLTable('
    for $customer in ora:view("customer")/ROW
       return $customer/LAST_NAME'
     columns "last_name" varchar2(4000) path '/LAST_NAME');

last_name
---------
Smith
Jackson
Peters
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top