0

这是我用于创建表和插入 xml 列的代码。

--drop 架构

BEGIN
DBMS_XMLSCHEMA.DELETESCHEMA(
   'http://xmlns.oracle.com/xdb/schemas/HZHE18/handBook',
   dbms_xmlschema.DELETE_CASCADE_FORCE);
END;

--注册二进制模式

BEGIN
DBMS_XMLSCHEMA.REGISTERSCHEMA(SCHEMAURL => 'handBook',
schemadoc => '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

<xs:annotation>
    <xs:documentation> Author: Andy(Haichuan) Zheng Schema for Web Handbook data
    </xs:documentation>
</xs:annotation>

<xs:simpleType name="code-style">
    <xs:restriction base="xs:string">
        <xs:pattern value="[A-Z][A-Z][A-Z][0-9][0-9][0-9][0-9]"/>
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="level-style">
    <xs:restriction base="xs:string">
        <xs:enumeration value="Undergraduate"/>
        <xs:enumeration value="Postgraduate"/>
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="email-style">
    <xs:restriction base="xs:string">
        <xs:pattern value="[^@]+@[^\.]+\..+"/>
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="point_lvl_type">
    <xs:restriction base="xs:positiveInteger">
        <xs:enumeration value="3"/>
        <xs:enumeration value="6"/>
        <xs:enumeration value="12"/>
        <xs:enumeration value="24"/>
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="offered_type">
    <xs:restriction base="xs:string">
        <xs:enumeration value="First semester"/>
        <xs:enumeration value="Second semester"/>
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="mode_type">
    <xs:restriction base="xs:string">
        <xs:enumeration value="Day"/>
        <xs:enumeration value="Evening"/>
        <xs:enumeration value="Off-campus"/>
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="campus_type">
    <xs:restriction base="xs:string">
        <xs:enumeration value="Caulfield"/>
        <xs:enumeration value="Gippsland"/>
        <xs:enumeration value="Sunway"/>
        <xs:enumeration value="Clayton"/>
        <xs:enumeration value="South Africa"/>
    </xs:restriction>
</xs:simpleType>

<xs:element name="handbook">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="page_info" minOccurs="1" maxOccurs="unbounded"/>
            <xs:element ref="unit" minOccurs="1" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="page_info">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="sub_date" type="xs:date"/>
        </xs:sequence>
        <xs:attribute name="author" type="xs:string"/>
    </xs:complexType>
</xs:element>

<xs:element name="unit">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="u_name" type="xs:string"/>
            <xs:element name="level" type="level-style"/>
            <xs:element ref="chief_examiner"/>
            <xs:element name="point_lvl" type="point_lvl_type"/>
            <xs:element name="prerequisites" type="code-style" maxOccurs="unbounded"
                minOccurs="0"/>
            <xs:element name="prohibitions" type="code-style" maxOccurs="unbounded"
                minOccurs="0"/>
            <xs:element ref="availability"/>
            <xs:element name="synopsis" type="xs:string"/>
            <xs:element name="assessment" type="xs:string"/>
            <xs:element name="contact_hours" type="xs:string"/>
        </xs:sequence>
        <xs:attribute name="code" type="code-style"/>
    </xs:complexType>
</xs:element>

<xs:element name="chief_examiner">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="email" type="email-style"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="availability">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="offered" type="offered_type" maxOccurs="unbounded"/>
            <xs:element name="mode" type="mode_type" maxOccurs="unbounded"/>
            <xs:element name="campus" type="campus_type" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

</xs:schema>',
LOCAL => TRUE,
GENTYPES => FALSE,
GENTABLES => FALSE,
FORCE => FALSE,
options => DBMS_XMLSCHEMA.REGISTER_BINARYXML);
end;

--drops 并清理回收站

drop table units_handbook purge;

PURGE RECYCLEBIN;

--create tables CREATE TABLE units_handbook ( unit_code varchar2(7) PRIMARY KEY, XML_detail XMLTYPE NOT NULL) XMLTYPE COLUMN XML_detail STORE AS BINARY XML XMLSCHEMA " http://xmlns.oracle.com/xdb/schemas/HZHE18/handBook " ELEMENT "手册”;

-- 填充表格

insert into units_handbook values('FIT2077', 
'<handbook>
    <page_info author = "Haichuan Zheng">
        <sub_date>2013-05-28</sub_date>
    </page_info>
    <unit code="FIT2077">
        <u_name>Advanced Data Management</u_name>
        <level>Undergraduate</level>
        <chief_examiner>
             <name>Mr Lindsay Smith</name>
             <email>Lindsay.Smith@monash.edu</email>
        </chief_examiner>
        <point_lvl>6</point_lvl>
        <prerequisites>FIT1004</prerequisites>
       <prerequisites>CSE2132</prerequisites>
    <prohibitions>CSE3000</prohibitions>
        <prohibitions>FIT3118</prohibitions>
    <prohibitions>FIT4038</prohibitions>
    <availability>
        <offered>First semester</offered>
        <mode>Day</mode>
        <campus>Caulfield</campus>
    </availability>
    <synopsis><![CDATA[This unit extends the study from FIT1004 Data management. 
        FIT2077 will introduce more advanced concepts in the areas of database design, SQL, 
        query optimisation and the handling of unstructured data (XML) both externally
        and within a database. The issue of "Big Data" and the role played by BI technologies 
        and data warehouses will be explored.]]></synopsis>
    <assessment>Examination (3 hours): 60%; In-semester assessment: 40%</assessment>
    <contact_hours>2 hrs lectures/wk, 2 hrs laboratories/wk</contact_hours>
</unit>
</handbook>');

insert into units_handbook values('FIT4038', 
'<handbook>
<page_info author = "Haichuan Zheng">
    <sub_date>2013-05-28</sub_date>
</page_info>
<unit code="FIT4038">
    <u_name>Database management and implementation</u_name>
    <level>Postgraduate</level>
    <chief_examiner>
        <name>Associate Professor David Taniar</name>
        <email>David.Taniar@monash.edu</email>
    </chief_examiner>
    <point_lvl>6</point_lvl>
    <prerequisites>FIT9019</prerequisites>
    <prerequisites>FIT9003</prerequisites>
    <prohibitions>FIT3118</prohibitions>
    <prohibitions>CSE3000</prohibitions>
    <availability>
        <offered>First semester</offered>
        <mode>Day</mode>
        <campus>Caulfield</campus>
    </availability>
    <synopsis><![CDATA[This unit looks at the design and implementation issues of 
        database management systems. Advanced database design using the object-relational approach 
        and multi-dimensional database design are explored. Record, file 
        and index structures are dealt with at the basic level. 
        Higher level details of consistency, atomicity and durability are
        introduced along with modern trends in databases.]]></synopsis>
    <assessment>Examination (3 hours): 60%; In-semester assessment: 40%</assessment>
    <contact_hours>2 hrs lectures/wk, 2 hrs laboratories/wk</contact_hours>
</unit>
</handbook>');

我的问题是,当我想检索 xml 格式中的所有 u_name 时,我收到此错误消息 ORA-01427: single-row subquery returns more than one row

这是我的代码

  select xmlelement("unit",xmlelement("u_name",
  (
   SELECT
      EXTRACTVALUE(u.XML_detail, '/handbook/unit/u_name')
    FROM
       units_handbook u
 )
  ) )
  from units_handbook;
4

1 回答 1

0

That's because your inner query returns more than one row (as it runs on the whole table).

Why can't you just do:

select xmlelement("unit",xmlelement("u_name",EXTRACTVALUE(u.XML_detail, '/handbook/unit/u_name')
    ))
  from units_handbook u;

if you must have it in the inner query (in the select clause) then you somehow need to connect the records or be sure you have only one row.
as

select xmlelement("unit",xmlelement("u_name",
  (
   SELECT
      EXTRACTVALUE(u.XML_detail, '/handbook/unit/u_name')
    FROM
       units_handbook u
    WHERE u.id = ou.id
 )
  ) )
  from units_handbook ou;

Here is a sqlfiddle demo

于 2013-05-30T08:31:00.687 回答