0

我正在使用 Oracle 11G。我有一个 PL/SQL 过程,它使用 xquery 从表中的 XMLTYPE 列读取 XML。XML 包含 DEPARTMENT 及其 SECTIONS 的数据。DEPARTMENT 与 SECTIONS 有一对多的关系,即一个 DEPARTMENT 可以有一个或多个 SECTIONS,并且可能存在 DEPARTMENT 没有任何 SECTIONS 的情况。

XML 的结构是这样的,<DATA>标签标识一个部门及其相应的部分。

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
4

1 回答 1

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
于 2013-05-02T12:22:46.693 回答