Search code examples
sqloracleoracle10gora-00932

How can I use REF to reference row objects?


I have two entities, Customer and Order, both of which I have created types for. The SQL type declarations are:

CREATE OR REPLACE TYPE  "CUSTOMERTYPE" AS OBJECT (
customerId CHAR(6),
name VARCHAR2(50),
address VARCHAR2(255),
telephone CHAR(11)
);

CREATE OR REPLACE TYPE  "ORDERTYPE" AS OBJECT (
customer REF CustomerType,
orderId CHAR(10),
orderDate DATE,
totalCost FLOAT
);

The idea being that a customer can place 1..* orders. An order is placed by 1..1 customer.

I've also created CustomerTable and OrderTable tables like so:

CREATE TABLE  "CUSTOMERTABLE" OF  "CUSTOMERTYPE";
CREATE TABLE  "ORDERTABLE" OF  "ORDERTYPE";

There is an entry in CustomerTable with customerId = '123456'. When I execute the following:

INSERT INTO OrderTable
  SELECT OrderType (REF(c), '1234567890', '02-Nov-2009', 99)
    FROM CustomerTable c
    WHERE c.customerId = '123456';

Oracle reports that the row has been inserted. However, when I check the data I get error:

ORA-00932: inconsistent datatypes: expected NUMBER got REF MILKPLUSVELLOCET.CUSTOMERTYPE

Any help would be greatly appreciated.


Solution

  • What do you mean by 'when I check the data'?

    This works on my machine:

    declare
      cust customertype;
    begin
      cust := new customertype('111111', 'Mr. Burns', '42 Mil,ky Way', '+41 75 000');
      insert into customertable values cust;
    end;
    /
    
    
    INSERT INTO OrderTable
      SELECT OrderType (REF(c), '1234567890', '02-Nov-2009', 99)
          FROM CustomerTable c
              WHERE c.customerId = '111111';
    
    --
    
    select * from ordertable;
    
    select o.customer.name from ordertable o where o.orderid = '1234567890';