Oracle - 选择(复杂查询)到用户定义的集合中

时间:2016-02-05 05:08:00

标签: database oracle

是否可以在用户定义的集合中选择一些数据?我创建了与查询返回相同的记录结构,然后尝试在我的表中选择数据。

procedure MY_PROC(md number, pPERIOD date) is 
TYPE MainRecType IS RECORD (
  addr varchar2(100),
  custom x_custom_table.custom%type,      
  id_cn number,
  iddb_cn number
);
TYPE MainTable IS TABLE OF MainRecType
  INDEX BY BINARY_INTEGER;  

begin

  select * bulk collect into MainTable from //ERROR!
  (
    select a.address addr, x.custom custom, b.id id_cn, b.id_db iddb_cn
    from AddressTable a, x_custom_table x, BuildTable b
    where a.id_build=b.id and a.id_x=x.id and b.period=pPeriod and b.md=md
  );      

end;

它说PLS-00321: expression 'MainTable' is inappropriate as the left hand side。如果可能的话,我做错了什么?

1 个答案:

答案 0 :(得分:0)

MainTable是一种集合类型。您需要声明该集合类型的实例以将数据提取到

procedure MY_PROC(md number, pPERIOD date) 
is 
  TYPE MainRecType IS RECORD (
    addr varchar2(100), 
    custom x_custom_table.custom%type,      
    id_cn number,
    iddb_cn number
  );
  TYPE MainTable IS TABLE OF MainRecType
    INDEX BY BINARY_INTEGER;  
  l_collection MainTable;
begin
  select a.address addr, x.custom custom, b.id id_cn, b.id_db iddb_cn
    bulk collect into l_collection 
    from AddressTable a, 
         x_custom_table x, 
         BuildTable b
   where a.id_build=b.id 
     and a.id_x=x.id 
     and b.period=pPeriod 
     and b.md=md;

end;

当然,我认为在您的实际代码中,一旦填充它,您将对该集合执行某些操作。