Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Dmitriy.

Asked: February 01, 2017 - 2:24 pm UTC

Last updated: September 19, 2018 - 10:21 am UTC

Version: 12.1.0.1.0

Viewed 1000+ times

You Asked

Hi,
I want to parse xml like:

<xml>
<field1>value1</field1>
<field2>value2</field2>
<field3>value3</field3>
</xml>

I write query, using xmltable and unpivot:
select *
  from (select p.field1,
               p.field2,
               p.field3
          from (select xmltype('<xml><field1>value1</field1><field2>value2</field2><field3>value3</field3></xml>') as xml
                  from dual) t,
               xmltable('/xml' passing t.xml columns 
                        field1 varchar2(100) path 'field1',
                        field2 varchar2(100) path 'field2',
                        field3 varchar2(100) path 'field3'
               ) p
         )
unpivot (value for field in (field1, field2, field3))

I expect to receive 3 rows, but returns only 1, why?
Is works correctly, if I add "union all" and "select from dual where 1=2" like:
select *
  from (select p.field1,
               p.field2,
               p.field3
          from (select xmltype('<xml><field1>value1</field1><field2>value2</field2><field3>value3</field3></xml>') as xml
                  from dual) t,
                       xmltable('/xml' passing t.xml columns 
                       field1 varchar2(100) path 'field1',
                       field2 varchar2(100) path 'field2',
                       field3 varchar2(100) path 'field3'
                ) p
                union all
                select '1' as column1,
                       '1' as column2,
                       '1' as column3
                  from dual where 1=2
        )
unpivot (value for field in (field1, field2, field3))

What to do? This is a bug?
I want to write code without unnecessary constructions

and Chris said...

There's a known bug related to this. See MOS note 2223258.1 for details.

You can fix it with the one-off patch:

Patch 14641856: UNPIVOT OPERATION ON VIEW WITH XMLTABLE IS MISSING ROWS


Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Review

Dmitriy Shabalin, February 02, 2017 - 6:11 am UTC

Thank you so much

Need more info on the Fix

VenkataThulasi B, September 18, 2018 - 1:53 pm UTC

Hi Team,

I am facing a similar issue. Could you please let us know whether any fix is available for this bug.
Chris Saxon
September 18, 2018 - 4:30 pm UTC

Gonna have to repeat my original answer for this one...

You can fix it with the one-off patch:

Patch 14641856: UNPIVOT OPERATION ON VIEW WITH XMLTABLE IS MISSING ROWS

Workaround?

Stew Ashton, September 19, 2018 - 6:55 am UTC

On version 12.2.0.1.0 the bug still exists, but there seems to be a workaround using "where rownum >= 1" to materialize the output from XMLTABLE.
select *
  from (select p.field1,
               p.field2,
               p.field3
          from (select xmltype('<xml><field1>value1</field1><field2>value2</field2><field3>value3</field3></xml>') as xml
                  from dual) t,
               xmltable('/xml' passing t.xml columns 
                        field1 varchar2(100) path 'field1',
                        field2 varchar2(100) path 'field2',
                        field3 varchar2(100) path 'field3'
               ) p
          where rownum >= 1
         )
unpivot (value for field in (field1, field2, field3));

FIELD  VALUE
------ ------
FIELD1 value1
FIELD2 value2
FIELD3 value3

Chris Saxon
September 19, 2018 - 10:21 am UTC

Thanks for coming up with the workaround Stew. FWIW it looks like this patch is included in 18c.