2013-05-30 4 views
0

вот мой код для создания таблиц и вставки столбцов xml.xml в оракуле встречает однострочный подзапрос возвращает более одной строки

--drop схема

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

--register бинарной схемы

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="[^@][email protected][^\.]+\..+"/> 
    </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 таблицы CREATE TABLE units_handbook ( unit_code varchar2 (7) ПЕРВИЧНЫЙ КЛЮЧ, XML_detail XmlType NOT NULL) XmlType COLUMN XML_detail STORE AS BINARY XML XMLSCHEMA "http://xmlns.oracle.com/xdb/schemas/HZHE18/handBook" ЭЛЕМЕНТ "руководство";

- заполнять таблицы

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>[email protected]</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>[email protected]</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>'); 

мой вопрос, когда я хочу, чтобы получить все u_name в пределах формата XML я получаю эту ошибку, говоря ORA-01427: однорядные подзапрос возвращает более одной строки

вот мой код

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

ответ

0

Это потому, что ваш внутренний запрос возвращает более одной строки (поскольку она работает на всей таблице).

Почему вы не можете просто сделать:

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

если вы должны иметь его во внутреннем запросе (при выборе пункта), то вы как-то нужны соединить записи или убедитесь, что у вас есть только один ряд ,
в

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

+0

круто, я был глуп, лол. –

Смежные вопросы