pl/sql--集合和记录

发布于 2024-10-17 19:22:28 字数 546 浏览 3 评论 0原文

我有两个表,名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无戏配角 2024-10-24 19:22:28

jonearles,是的,最好的方法是使用常规 SQL - 插入 + 选择。

但是,如果您有一些需要在 PL/SQL 中逐行处理的业务逻辑,您可以使用与您所拥有的稍有不同的变体:

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 .. l_pc.count loop 
    l_ab(j).cuno := l_pc(j).cust_no;
    -- perhaps some other processing here...
  end loop;

  FORALL i in 1..l_ab.count
    insert into ab values l_ab(i);

end;

最后的 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:

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 .. l_pc.count loop 
    l_ab(j).cuno := l_pc(j).cust_no;
    -- perhaps some other processing here...
  end loop;

  FORALL i in 1..l_ab.count
    insert into ab values l_ab(i);

end;

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.

ㄖ落Θ余辉 2024-10-24 19:22:28

您不能批量收集到关联数组中。您应该使用基于列类型而不是表行类型定义的嵌套表。

declare
    type Ty_Handoff_Pc is table of aa.cust_no%type;
    Type sam Is Table Of ab%rowtype;
    l_pc Ty_Handoff_Pc;
begin
    select distinct cust_no bulk collect into l_pc from aa;

    for j in 1 .. l_pc.count loop
        insert into ab values(l_pc(j));
    end loop;
end;
/

编辑:正如 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.

declare
    type Ty_Handoff_Pc is table of aa.cust_no%type;
    Type sam Is Table Of ab%rowtype;
    l_pc Ty_Handoff_Pc;
begin
    select distinct cust_no bulk collect into l_pc from aa;

    for j in 1 .. l_pc.count loop
        insert into ab values(l_pc(j));
    end loop;
end;
/

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.

熊抱啵儿 2024-10-24 19:22:28
declare 
type taba is recor(cust_no aa.cust_no%type);
type tabb is table of taba;
custno_t tabb;
begin 
select cust_no bulk collect into custno_t from aa;
forall i in custno_t.first..custno_t.last
insert into bb values custno_t(i);
commit;
end;
declare 
type taba is recor(cust_no aa.cust_no%type);
type tabb is table of taba;
custno_t tabb;
begin 
select cust_no bulk collect into custno_t from aa;
forall i in custno_t.first..custno_t.last
insert into bb values custno_t(i);
commit;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文