pl/sql--集合和记录
我有两个表,名为“AA”和“AB”
- 。在表“AA”中,我有 cust_no、名称、地址等列。
- 在表“AB”中,我有一列
cuno
我需要从“AA”表中获取 cust_no 并将其放入“AB”表的 cuno 列中。
我在这里尝试了一些代码...
declare
Type Ty_Handoff_Pc Is Table Of aa%Rowtype Index By Binary_Integer;
Type sam Is Table Of ab%rowtype;
l_pc Ty_Handoff_Pc;
l_ab sam;
begin
select distinct cust_no bulk collect into l_pc from aa;
for j in 1 .. 10 loop
l_ab(j) := l_pc(j).cust_no;
insert into ab values l_ab(j);
end loop;
end;
提前致谢
I have two tables called "AA" and "AB"
- In table "AA" I have columns like cust_no, name, address, etc..
- In table "AB" I have one column
cuno
I need to fetch cust_no from "AA" table and put it into cuno column of "AB" table.
I tried some code here...
declare
Type Ty_Handoff_Pc Is Table Of aa%Rowtype Index By Binary_Integer;
Type sam Is Table Of ab%rowtype;
l_pc Ty_Handoff_Pc;
l_ab sam;
begin
select distinct cust_no bulk collect into l_pc from aa;
for j in 1 .. 10 loop
l_ab(j) := l_pc(j).cust_no;
insert into ab values l_ab(j);
end loop;
end;
THANKS IN ADVANCE
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jonearles,是的,最好的方法是使用常规 SQL - 插入 + 选择。
但是,如果您有一些需要在 PL/SQL 中逐行处理的业务逻辑,您可以使用与您所拥有的稍有不同的变体:
最后的 FORALL 的优点是插入是使用批量完成的in-bind 数组,因此只需对 SQL 引擎进行一次调用,而不是对每条记录调用一次。
jonearles, is right, the best way is to use regular SQL - insert + select.
If you have some business logic that requires row-by-row processing in PL/SQL, however, you can use a slightly different variant of what you had:
The advantage of the FORALL at the end is that the insert is done using a bulk in-bind array, so only one call to the SQL engine instead of one for each record.
您不能批量收集到关联数组中。您应该使用基于列类型而不是表行类型定义的嵌套表。编辑:正如 Jeffrey Kemp 指出的那样,您可以批量收集到关联数组中。如果这是真正的代码,并且不仅仅是为了学习,那么您绝对应该使用常规 SQL 来完成它。
insert into ab(custno) select different cust_no from aa
比使用 PL/SQL 快得多。You cannot bulk collect into an associative array. You should use a nested table defined based on the column type instead of the table rowtype.Edit: As Jeffrey Kemp pointed out, you can bulk collect into an associative array.If this is real code, and not just for learning, you should definitely do it in regular SQL.
insert into ab(custno) select distinct cust_no from aa
will be much faster than using PL/SQL.