将数据插入嵌套表中

时间:2013-05-15 03:23:38

标签: oracle reference insert nested-table object-relational-model

我可以帮助将值插入表格中。该表称为PurchaseOrder_objtab。这是表格的类型:

CREATE TYPE PurchaseOrder_objtyp AUTHID CURRENT_USER AS OBJECT
(
  PONo NUMBER,
  CUST_ref REF Customer_objtyp,
  OrderDate DATE,
  ShipDate DATE,
  LineItemList_ntab LineItemList_ntabtyp,
  ShipToAddr_obj Address_objtyp
)
/

LineItemList_ntab是一个嵌套表。

以下是LineItemList_ntabtyp的创建代码:

CREATE TYPE LineItem_objtyp AS OBJECT (
  LineItemNo NUMBER,
  Stock_ref REF StockItem_objtyp,
  Quantity NUMBER,
  Discount NUMBER
)
/

CREATE TYPE LineItemList_ntabtyp AS TABLE of LineItem_objtyp
/

以下是一些可行的插入代码:

INSERT INTO PurchaseOrder_objtab
SELECT 1008, REF(C),
SYSDATE, '12-MAY-1999',
LineItemList_ntabtyp(),
NULL
FROM Customer_objtab C 
WHERE C.CustNo = 1;

在上面的代码中,LineItemList_ntab是一个空的LineItemList_ntabtyp。我想在这个嵌套表中添加值,而不是在INSERT INTO代码中为空。

以下是我尝试使用以下代码插入值的一些代码:

INSERT INTO PurchaseOrder_objtab
SELECT 1008, REF(C),
SYSDATE, '12-MAY-1999',
LineItemList_ntabtyp(1, REF(StckItem), 10, 1) FROM Stock_objtab StckItem WHERE StckItem.StockNo = 1004,
NULL
FROM Customer_objtab C 
WHERE C.CustNo = 1;

INSERT INTO PurchaseOrder_objtab
SELECT 1008, REF(C),
SYSDATE, '12-MAY-1999',
INSERT INTO LineItemList_ntab SELECT 1, REF(StckItem),10,1 FROM Stock_objtab StckItem WHERE StckItem.StockNo = 1004, 
NULL
FROM Customer_objtab C 
WHERE C.CustNo = 1;

上面的第一个插入语句产生以下错误:

  

SQL错误:ORA-00933:SQL命令未正确结束

上面的第二个插入语句产生以下错误:

  

SQL错误:ORA-00936:缺少表达式

我可以帮助您成功地将值插入LineItemList_ntab嵌套表吗?

更新

我有这段代码来插入数据:

INSERT INTO PurchaseOrder_objtab
SELECT 
1008, 
REF(C),
SYSDATE, 
'12-MAY-1999',
(SELECT LineItemList_ntabtyp(LineItem_objtyp(1, REF(StckItem), 10, 1)) FROM Stock_objtab StckItem WHERE StckItem.StockNo = 1004),
NULL
FROM Customer_objtab C 
WHERE C.CustNo = 1;

我现在收到以下错误:

  

SQL错误:ORA-22979:无法INSERT对象视图REF或用户定义的REF   22979. 00000 - “无法INSERT对象视图REF或用户定义的REF”   *原因:尝试在a中插入对象视图REF或用户定义的REF              创建REF列以存储系统生成的REF值“   *操作:确保要插入的REF不是来自对象视图              或者来自用户定义的REF专栏

我很确定这个错误是因为我可能错误地创建了表,这是我的表创建代码,用于PurchaseOrder_objtab:

CREATE TABLE PurchaseOrder_objtab OF PurchaseOrder_objtyp(
PRIMARY KEY (PONo),
FOREIGN KEY (Cust_ref) REFERENCES Customer_objtab)
OBJECT ID PRIMARY KEY

  NESTED TABLE LineItemList_ntab STORE AS PoLine_ntab(
(PRIMARY KEY(NESTED_TABLE_ID, LineItemNo))
ORGANIZATION INDEX COMPRESS)
RETURN AS LOCATOR
/

以下是创建嵌套表的代码:

CREATE TYPE LineItemList_ntabtyp AS TABLE of LineItem_objtyp
/

UPDATE2

此代码成功地向表中添加了信息:

INSERT INTO PurchaseOrder_objtab
SELECT 1009, REF(C),
SYSDATE, '10-MAY-1999',
LineItemList_ntabtyp(),
NULL
FROM Customer_objtab C 
WHERE C.CustNo = 1;

因此该错误与LineItemList_ntabtyp有关。

这是表创建代码:

CREATE TABLE PurchaseOrder_objtab OF PurchaseOrder_objtyp(
PRIMARY KEY (PONo),
FOREIGN KEY (Cust_ref) REFERENCES Customer_objtab)
OBJECT ID PRIMARY KEY

NESTED TABLE LineItemList_ntab STORE AS PoLine_ntab((PRIMARY KEY(NESTED_TABLE_ID,LineItemNo)) ORGANIZATION INDEX COMPRESS) RETURN AS LOCATOR/

我确定该错误与上表的OBJECT IDENTIFIER IS PRIMARY KEY有关。

UPDATE3

感谢您的代码。如何在嵌套表中插入多个值。

您的一行代码是:

(SELECT LineItemList_ntabtyp(LineItem_objtyp(1, REF(StckItem), 10, 1)) FROM Stock_objtab WHERE StockNo = 1004),
NULL)

如果我想添加两个商品,StockNo 1004和1005,怎么样?它是这样的:

(SELECT LineItemList_ntabtyp(LineItem_objtyp(1, REF(StckItem), 10, 1)) FROM Stock_objtab WHERE StockNo = 1004) AND (SELECT LineItemList_ntabtyp(LineItem_objtyp(2, REF(StckItem), 10, 1)) FROM Stock_objtab WHERE StockNo = 1005),
NULL)

UPDATE4

如果,我向嵌套表中添加了三个项目,“FROM DUAL”语句是否保持为“FROM DUAL”,还是更改为反映三个项目的内容?此外,“UNION ALL”语句是否显示如下:

(
SELECT LineItemList_ntabtyp(
    SELECT LineItem_objtyp(1, REF(StckItem), 10, 1) FROM Stock_objtab WHERE StockNo = 1004         
    UNION ALL 
    SELECT LineItem_objtyp(2, REF(StckItem), 10, 1) FROM Stock_objtab WHERE StockNo = 1005 
    UNION ALL 
    SELECT LineItem_objtyp(3, REF(StckItem), 10, 1) FROM Stock_objtab WHERE StockNo = 1006
)
FROM DUAL
)

1 个答案:

答案 0 :(得分:2)

您应该在select子查询中使用类型构造函数来填充您的场合中的嵌套表:

INSERT INTO PurchaseOrder_objtab
SELECT 
    1008, 
    REF(C),
    SYSDATE, 
    '12-MAY-1999',
    (SELECT LineItemList_ntabtyp(LineItem_objtyp(1, REF(StckItem), 10, 1)) FROM Stock_objtab WHERE StockNo = 1004),
    NULL
FROM Customer_objtab C 
WHERE C.CustNo = 1;

<强>更新

我不确定你正确地完成任务的事实(我的意思是将对象类型作为表格存储在我脑海中是没用的,我会使用可管理的普通表而不是这样的结构。

INSERT INTO PurchaseOrder_objtab
SELECT PurchaseOrder_objtyp(
    1008, 
    REF(C),
    SYSDATE, 
    '12-MAY-1999',
    (SELECT LineItemList_ntabtyp(LineItem_objtyp(1, REF(StckItem), 10, 1)) FROM Stock_objtab WHERE StockNo = 1004),
    NULL)
FROM Customer_objtab C 
WHERE C.CustNo = 1;

更新2:

如果您急于在嵌套表中插入多个值,请使用枚举(如果值是手写)或子查询:

(
    SELECT LineItemList_ntabtyp(
        SELECT LineItem_objtyp(1, REF(StckItem), 10, 1) FROM Stock_objtab WHERE StockNo = 1004         
        UNION ALL 
        SELECT LineItem_objtyp(2, REF(StckItem), 10, 1) FROM Stock_objtab WHERE StockNo = 1005
    ) 
    FROM DUAL
)