我是PL / SQL的新手,我正在编写批量插入。我有一个嵌套的游标,在内部游标内部我想将记录添加到一个集合中,然后我将执行批量插入。我将集合创建为:
TYPE mydata_tab IS TABLE OF mydata%ROWTYPE INDEX BY BINARY_INTEGER;
v_mydata_tab mydata_tab;
然后我的游标看起来像:
FOR rec_one IN cursor_one LOOP
strKey := rec_one.key;
FOR rec_two IN cursor_two LOOP
-- here is where I want to add a record to v_mydata_tab, that uses properties of both rec_one and rec_two
-- something like SELECT rec_one.key, rec_one.a, rec_two.b INTO v_mydata_tab;
END LOOP;
END LOOP;
我玩过SELECT INTO
和INSERT INTO
,但似乎无法弄清楚如何让它发挥作用。
答案 0 :(得分:1)
你可以这样做:
n := 0; -- n is integer variable declared above
FOR rec_one IN cursor_one LOOP
strKey := rec_one.key;
FOR rec_two IN cursor_two LOOP
n := n + 1;
v_mydata(n).key := rec_one.key;
v_mydata(n).a := rec_two.a;
v_mydata(n).b := rec_two.b;
END LOOP;
END LOOP;
当然,如果你可以将2个游标的SQL组合成一个select语句,你可以这样做:
select x.key, y.a, y.b
bulk collect into v_mydata
from ...;
或者如果数据太多:
open cur;
loop
fetch cur bulk collect into v_mydata limit 1000;
...
exit when cur%notfound;
end loop;
close cur;
答案 1 :(得分:1)
你只需使用作业。 rec_one
和rec_two
是记录类型的局部变量。
FOR rec_one IN cursor_one LOOP
strKey := rec_one.key;
FOR rec_two IN cursor_two LOOP
i := v_mydata_tab.COUNT+1; -- declare i above as local var
v_mydata_tab( i ).key := rec_one.key;
v_mydata_tab( i ).a := rec_one.a;
v_mydata_tab( i ).b := rec_two.b;
END LOOP;
END LOOP;