oracle - 精确提取在过程中返回的行数超过了请求的行数
问题描述
当嵌套表包含多个值时,调用过程得到以下错误。
是否可以将 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;
解决方案
由于您是批量插入,因此您必须从第一个 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
*/
推荐阅读
- react-native - 反应原生推送通知 OneSignal/Firebase
- firebase - 如何通过curl通过REST从firebase获取数据?
- python - 尝试加载图像数据集并将其转换为数组时出现未知文件
- css - 固定行数,如果有更多内容,它应该使用 css 网格转到下一列
- javascript - React 本机组件不更新
- ponyorm - Pony ORM - 实体属性在实例创建时是可选的,但在数据库保存时是必需的
- javascript - 如何将 IndexedDB 值数据检索到变量?
- r - 计算数据框中(长)周末的天数
- javascript - 在 Facebook 可播放广告上使用 Google 地图,无需外部调用
- vb.net - 编号或编号 Crystal Report / vb.net