首页 > 解决方案 > 如何在 PL/SQL 中使用 for 循环创建游标来传输记录?

问题描述

使用 FOR 循环创建游标,但我唯一的问题是我们如何将已获取的每一行转移到另一个表中?是否可以?

在我们从特定表中获取行之后,使用 PL/SQL 块并包含一个 FOR 循环,之后我们希望将已获取的行转移到其他表中。

标签: oracleplsqloracle11goracle-sqldeveloper

解决方案


这是一个将Scott 表中的值复制DEPT到表中的示例TEST(也由 Scott 拥有)。

SQL> create table test (deptno number, dname varchar2(20), loc varchar2(20));

Table created.

SQL> begin
  2    for cur_r in (select deptno, dname, loc
  3                  from dept
  4                  where deptno <= 30
  5                 )
  6    loop
  7      insert into test (deptno, dname, loc)
  8        values (cur_r.deptno, cur_r.dname, cur_r.loc);
  9    end loop;
 10  end;
 11  /

PL/SQL procedure successfully completed.

SQL> select * from test;

    DEPTNO DNAME                LOC
---------- -------------------- --------------------
        10 ACCOUNTING           NEW YORK
        20 RESEARCH             DALLAS
        30 SALES                CHICAGO

SQL>

推荐阅读