首页 > 解决方案 > 精确提取在过程中返回的行数超过了请求的行数

问题描述

当嵌套表包含多个值时,调用过程得到以下错误。

是否可以将 NEW_ID 变量放在 FORALL 块或其他解决方案中?

ORA-01422: 精确提取返回的行数多于请求的行数

procedure EMP_INSERT (
    P_EMP employees_nt
)
as
   NEW_ID number;

begin

   FORALL i IN 1 .. P_EMP.COUNT
        Insert into test (val_1) values (P_EMP(i)) RETURNING new_id into NEW_ID;
        Insert into test2 (val_2) values (NEW_ID);   
        //Is it possible put here NEW_ID to null?

end;    
    
    

标签: oracleplsql

解决方案


由于您是批量插入,因此您必须从第一个 FORALL 批量返回值。然后,您可以使用它们来执行另一个 FORALL 以插入到您的第二个表中。

drop table t2;
drop table t;
drop sequence t2_seq;
drop sequence t_seq;

create sequence t_seq start with 100;
create sequence t2_seq;
create table t (
       t_id  number default t_seq.nextval not null,
       val_1 varchar2(10)
);
create table t2 (
       t2_id number default t2_seq.nextval not null,
       t_id  number);
declare
  type t_val_tab is table of t.val_1%type;
  type t_id_tab is table of t.t_id%type;
  
  l_val_tab t_val_tab := t_val_tab();
  l_id_tab t_id_tab;
begin
  l_val_tab.extend(3);
  l_val_tab(1) := 'a';
  l_val_tab(2) := 'b';
  l_val_tab(3) := 'c';
  
  forall i in l_val_tab.first .. l_val_tab.last
  insert into t(val_1) values (l_val_tab(i))
  returning t.t_id bulk collect into l_id_tab;
  
  forall i in l_id_tab.first .. l_id_tab.last
  insert into t2(t_id) values (l_id_tab(i));
end;
/

select * from t2;
/*
t2  t
1   100
2   101
3   102
*/

推荐阅读