2009-11-03 67 views
0

我有兩個實體,Customer和Order,這兩個實體都是我爲其創建的類型。 SQL類型聲明如下:如何使用REF引用行對象?

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 
); 

想法是,客戶可以放置1 .. *訂單。訂單由1..1個客戶提供。

我還創建了CustomerTable的和OrderTable表像這樣:

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

有一個在CustomerTable的與客戶ID = '123456' 的條目。當我執行以下操作:

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

Oracle報告該行已被插入。然而,當我檢查數據,我得到錯誤:

ORA-00932:不一致的數據類型:預期數量得到了REF MILKPLUSVELLOCET.CUSTOMERTYPE

任何幫助將不勝感激。

回答

0

你是什麼意思'當我檢查數據'?

此工程在我的機器上:

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';