我正在使用Oracle 11G。 我有一个PL / SQL过程,它使用xquery从表中的XMLTYPE列读取XML。 XML包含DEPARTMENT及其SECTIONS的数据。 DEPARTMENT与SECTIONS有一对多的关系,即DEPARTMENT可以有一个或多个SECTIONS,并且可能存在DEPARTMENT没有任何SECTIONS的情况。
XML的结构使得<DATA>
标记标识DEPARTMENT及其相应SECTIONS的集合。
XML
<ROWSET>
<DATA>
<DEPARTMENT>
<DEPARTMENT_ID>DEP1</DEPARTMENT_ID>
<DEPARTMENT_NAME>myDEPARTMENT1</DEPARTMENT_NAME>
</DEPARTMENT>
<SECTIONS>
<SECTIONS_ID>6390135666643567</SECTIONS_ID>
<SECTIONS_NAME>mySection1</SECTIONS_NAME>
</SECTIONS>
<SECTIONS>
<SECTIONS_ID>6390135666643567</SECTIONS_ID>
<SECTIONS_NAME>mySection2</SECTIONS_NAME>
</SECTIONS>
</DATA>
<DATA>
<DEPARTMENT>
<DEPARTMENT_ID>DEP2</DEPARTMENT_ID>
<DEPARTMENT_NAME>myDEPARTMENT2</DEPARTMENT_NAME>
</DEPARTMENT>
<SECTIONS>
<SECTIONS_ID>63902</SECTIONS_ID>
<SECTIONS_NAME>mySection1</SECTIONS_NAME>
</SECTIONS>
</DATA>
<DATA>
<DEPARTMENT>
<DEPARTMENT_ID>DEP3</DEPARTMENT_ID>
<DEPARTMENT_NAME>myDEPARTMENT3</DEPARTMENT_NAME>
</DEPARTMENT>
</DATA>
</ROWSET>
我正在使用的查询是: -
select d.department_id
, d.department_name
, s.sections_id
, s.sections_name
from xml_unit_download t
, xmltable(
'/ROWSET/DATA'
passing t.xml_file
columns
DEPARTMENT_ID varchar2(20) path 'DEPARTMENT/DEPARTMENT_ID'
, DEPARTMENT_NAME varchar2(30) path 'DEPARTMENT/DEPARTMENT_NAME'
, SECTIONS xmltype path 'SECTIONS'
) d
, xmltable(
'/SECTIONS'
passing d.sections
columns
SECTIONS_ID varchar2(20) path 'SECTIONS_ID'
, SECTIONS_NAME varchar2(30) path 'SECTIONS_NAME'
) s
where
t.Status = 7
现在这个Xquery没有获取DEP3的结果,我将如何迎合这个?
-------------的修订版 -------------
预期输出
DEPARTMENT_ID DEPARTMENT_NAME SECTIONS_ID SECTIONS_NAME
-------------------- ------------------------------ --------------------
DEP1 myDEPARTMENT1 6390135666643567 mySection1
DEP1 myDEPARTMENT1 6390135666643567 mySection2
DEP2 myDEPARTMENT2 63902 mySection1
DEP3 myDEPARTMENT3
答案 0 :(得分:2)
您可以将它们设为外连接:
...
from xml_unit_download t
, xmltable(
...
) (+) d
, xmltable(
...
) (+) s
...
给出:
DEPARTMENT_ID DEPARTMENT_NAME SECTIONS_ID SECTIONS_NAME
-------------------- ------------------------------ -------------------- ------------------------------
DEP1 myDEPARTMENT1 6390135666643567 mySection1
DEP1 myDEPARTMENT1 6390135666643567 mySection2
DEP2 myDEPARTMENT2 63902 mySection1
DEP3 myDEPARTMENT3