Question

Basically, I want to create references to records, one record type is a supertype of the other. I am a bit confused over it.

Here are my types:

 CREATE OR REPLACE TYPE  module_t AS OBJECT (
     moduleCode# char(4),
     moduleName char(10)
) NOT FINAL;
/

CREATE OR REPLACE TYPE  specialised_module_t under module_t (
     someSpecialAttribute char(10)
);
/

CREATE OR REPLACE TYPE  course_t AS OBJECT (
     courseCode# char(4),
     module1 REF module_t,
     module2 REF specialised_module_t
);
/

Here are my tables:

CREATE TABLE module_tab OF module_t(
    moduleCode# PRIMARY KEY NOT NULL
);

CREATE TABLE course_tab OF course_t(
    courseCode# PRIMARY KEY NOT NULL,
    scope for (module1) is module_tab,
    scope for (module2) is module_tab
);

I insert a record into the module table consisting of a standard module:

insert into module_tab values(
'm001', 'physics'
);

I insert a record into the module table consisting of a specialised module:

insert into module_tab
    values( specialised_module_t(
    'm002',
    'physics',
    'special'
    )
);

Here is the tricky bit:

I want to create a course record in the course table. In this record, the module1 attribute needs to reference module m001 (the first module record) and the module2 attribute needs to reference module m002

I have thought maybe it would look something like this:

INSERT into course_tab VALUES(
       'c001',
        SELECT REF(c) FROM module_tab c WHERE c.moduleCode# = 'm001',
        SELECT REF(c) FROM module_tab c WHERE c.moduleCode# = 'm002'
)

This just doesn't work :(

Here is the error message:

ERROR at line 3:
ORA-00936: missing expression

Obviously my syntax is wrong, but I don't know how to correct it :/

I have spent ages on this and I can't figure it out :(
Help will be much appreciated :D
Thanks

UPDATE:

With help from @a_horse_with_no_name my new code is:

INSERT into course_tab VALUES(
       'c001',
        (SELECT REF(c) FROM module_tab c WHERE c.moduleCode# = 'm001'),
        (SELECT treat(REF(c) as ref specialised_module_t) FROM module_tab c WHERE treat(values(c) as moduleCode).person# = 'm002')
);

But now I get the error:

ERROR at line 4:
ORA-00936: missing expression

please help, thanks :)

Was it helpful?

Solution

If you want to use a SELECT statement to insert a scalar value inside the VALUES clause, you need to put the SELECT statement betweeen parentheses:

INSERT into course_tab 
   (coursecode#, module1, module2)
VALUES 
   (
    'c001',
    (SELECT REF(c) FROM module_tab c WHERE c.moduleCode# = 'm001'),
    (SELECT REF(c) FROM module_tab c WHERE c.moduleCode# = 'm002')
   );

Although when I tried it with your example (which, by the way has still lots of other syntax errors) I got the error message:

ORA-00932: inconsistent datatypes: expected REF SPECIALISED_MODULE_T got REF MODULE_T [SQL State=42000, DB Errorcode=932]

I don't really know what to do with that though (as I said I haven't really worked with Oracle object types, and the table definition for course_tab is completely new to me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top