【发布时间】:2009-01-27 17:02:05
【问题描述】:
为更好地澄清而编辑:
2009 年 1 月 28 日添加:为了便于解释,我过度简化了代码,但是 select 语句很长很复杂,第二个依赖于第一个游标完成并循环后的第一个含义通过并创建插入,第二个选择实际上将第一个插入视为 where 子句的一部分。
这就是为什么我需要多次使用循环而不以任何方式组合选择。 当我按照我想要调用它们的顺序调用它们时,我需要它们运行,这让我回到了我原来的问题,是否可以重新使用具有不同光标的循环?
再次感谢。
我正在创建一个包 (Oracle 10),其中有 4 个不同的选择语句(可能还会有更多),我为所有这些语句创建了一个游标并获取我的数据。现在通常我会获取数据并创建一个 For 循环,一切都很好。
我的问题是我有 4 个不同的选择,但我想重新使用循环,以便我可以让光标 c2 使用相同的循环以及 c3 和 c4。所有这些都是游标从非常不同的选择中获取不同的信息,但它们都进入同一个表,我的插入语句在循环中。此外,我不能将所有选择连接在一起,它们必须在每个循环后按顺序完成
我在下面创建了一个带有 4 个循环的示例,但正如您所见,它们都是相同的,唯一的区别是:对于 r 在 c1 循环中,对于 r 在 c2 循环中...... 我认为必须有某种方法可以重用循环。我有一些想法,但都没有奏效。
cursor c1 is select info_a, info_b from table_x where info_g = 77;
cursor c2 is select info-a, info_b from table_x where info_g = 88;
cursor c3 is select info-a, info_b from table_y where info_j = 88;
cursor c4 is select info-a, info_b from table_y where info_j = 99;
Begin
For r in c1 loop
insert into fun_table (good_info, boring_info) values (r.info_a, r.info-b);
end loop;
commit;
For r in c2 loop
insert into fun_table (good_info, boring_info) values (r.info_a, r.info-b);
end loop;
commit;
For r in c3 loop
insert into fun_table (good_info, boring_info) values (r.info_a, r.info-b);
end loop;
commit;
For r in c4 loop
insert into fun_table (good_info, boring_info) values (r.info_a, r.info-b);
end loop;
commit;
end;
希望这更有意义,谢谢
我进行了编辑,然后出现了一些答案……抱歉。 原来的样子是这样的:
cursor c1 is select some_info, other_info from some_table where where some_thing = 'xyz';
cursor c2 is select some_info, other_info from some_table where where some_thing = 'abc';
For r in c1 loop
insert into fun_table (good_info, boring_info) values (r.some_info, r.other_info);
end loop;
【问题讨论】:
-
请发布您希望如何查看它(也许一些无法编译但让我们了解您想要什么的代码)
-
在 for 循环中: For r in c1 loop where it uses c1 我想传入任何游标,因此它可以是: For r in c2 loop or : For r in d1 loop or any set要循环的数据
-
你能展示不同的SQL吗?
-
为什么插入时需要游标?只需使用 insert...select... 语句。思考基于集合而不是基于行。