如何在嵌套表列中更新来自不同表的多行(批量)?

时间:2013-09-30 04:09:01

标签: oracle

我发现了许多示例,展示了如何使用嵌套表列创建表以及如何“手动”更新它们,如步骤1-5中详细说明的那样。

我想做什么: 直接/自动地从数据库中的其他表(NEW_TBL-具有相同结构)更新嵌套表列(示例中为“cust_info”),而不是手动逐个输入值。

(1)创建对象:

CREATE TYPE TEST_OBJECT_T AS OBJECT
(
x   NUMBER,
y   NUMBER 
);

(2)创建集合:

CREATE  TYPE TEST_OBJ_TBL IS TABLE OF TEST_OBJECT_T;

(3)使用嵌套表列

创建表
create table aaaTable
 (
 CUSTID number,
 cust_info TEST_OBJ_TBL
 )
 NESTED TABLE cust_info STORE AS xx_tbl
 ;

(4) - 插入数据

insert into AAATABLE  
 VALUES (1,TEST_OBJ_TBL(
                             TEST_OBJECT_T(33,77),
                             TEST_OBJECT_T(66,67),
                             TEST_OBJECT_T(320,999)
                            )
        );

+++++++++++++++++++++++++++++++++++++++++++++++ ++++++++

insert into AAATABLE  
 VALUES (2,TEST_OBJ_TBL(
                             TEST_OBJECT_T(444,555),
                             TEST_OBJECT_T(333,67),
                             TEST_OBJECT_T(111,978)
                            )
        );

(5)查询2见结果

 select T1.custid ,T2.*
  from AAATABLE T1 , table(T1. CUST_INFO) T2;

results :
CUSTID          X          Y
---------- ---------- ----------
         1         33         77 
         1         66         67 
         1        320        999 
         2        444        555 
         2        333         67 
         2        111        978

(6)创建新表,它将是更新嵌套表列的数据源)

create table new_tbl
(
X1  NUMBER,
Y1  NUMBER 
);

(7)将数据插入新表

insert into NEW_TBL values (222,444);
insert into NEW_TBL values (345,777);
insert into NEW_TBL values (867,222);

+++++++++++++++++++++++++++++++++

select * from NEW_TBL


   X 1         Y1
---------- ----------
       222        444 
       345        777 
       867        222 

问题:我可以批量将“new_tbl”数据插入到嵌套表列(“cust_info”)中(意思是 - 在第4阶段替换数据)                              的 TEST_OBJECT_T(33,77),                              TEST_OBJECT_T(66,67),                              TEST_OBJECT_T(320999) ) 我试图使用批量收集但没有成功。

由于 可人

1 个答案:

答案 0 :(得分:1)

您可以使用COLLECT功能(使用CAST)

update aaatable d
set d.cust_info = (select cast ( collect(TEST_OBJECT_T(x1,y1)) as TEST_OBJ_TBL ) from new_tbl)
where d.custid = 1;

Here is a sqlfiddle demo