SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
80 dba beijing
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> select * from tmp_dept;
未选定行
SQL>
declare 2
cursor cur_dept is select deptno from dept;
3 begin
4
for dept_rec in cur_dept
loop 5 insert into tmp_dept(deptno) values(dept_rec.deptno);
6
end loop;
7 end;
8 /
PL/SQL 过程已成功完成。
SQL> select * from tmp_dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10
20
30
40
80
SQL> truncate table tmp_dept;
表被截断。
SQL> create or replace procedure proc_tmp_dept
2 as
3
begin 4
declare --此declare不能少,pl/sql存储过程begin-end可以含多层,
--即可以嵌套多次
5 cursor cur_dept is select deptno from dept;
6
begin --此begin不能少,不写此begin与对应的end(即for loop代码块不用begin end括起来,会报编译错误)
7
for dept_rec in cur_dept
loop 8 insert into tmp_dept(deptno) values(dept_rec.deptno);
9
end loop; 10
end; 11
end; 12 /
过程已创建。
SQL> select * from tmp_dept;
未选定行
SQL> exec proc_tmp_dept;
PL/SQL 过程已成功完成。
SQL> commit;
提交完成。
SQL> select * from tmp_dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10
20
30
40
80
SQL>
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9240380/viewspace-713948/
,如需转载,请注明出处,否则将追究法律责任。