DBMS_OUTPUT.PUT_LINE 在无法显示它的存储过程或前端应用程序中没有多大意义(例如,尽管它在 Oracle Forms 或 Application Express 中进行了有效调用,但您不会在屏幕上看到任何东西)。
您真的不希望用户在 SQL*Plus 或 TOAD 或 SQL Developer 中运行您的过程,是吗?因此,请使用DBMS_OUTPUT.PUT_LINE 进行调试。
我猜这就是你可能正在使用的:
SQL> create or replace procedure p_test_2
2 (par_deptno in number)
3 is
4 begin
5 for cur_r in (select ename from emp where deptno = par_deptno) loop
6 dbms_output.put_line(cur_r.ename);
7 end loop;
8 end;
9 /
Procedure created.
SQL> exec p_test_2(10);
CLARK
KING
MILLER
PL/SQL procedure successfully completed.
SQL>
如果要将其与应该返回多个值的过程进行比较,那么一种选择是使用数据类型为SYS_REFCURSOR 的OUT 参数。就是这样;您会注意到它需要更多代码 - 过程本身和单独的 PL/SQL 块,它们将对结果进行一些操作。
SQL> create or replace procedure p_test3
2 (par_deptno in number, par_emp out sys_refcursor)
3 is
4 begin
5 open par_emp for select * from emp where deptno = par_deptno;
6 end;
7 /
Procedure created.
SQL>
SQL> declare
2 l_emp sys_refcursor;
3 l_rec emp%rowtype;
4 begin
5 p_test3(10, l_emp);
6
7 loop
8 fetch l_emp into l_rec;
9 exit when l_emp%notfound;
10 -- You'd do something with those values here; I'm just displaying ENAME
11 dbms_output.put_line(l_rec.ename);
12 end loop;
13 end;
14 /
CLARK
KING
MILLER
PL/SQL procedure successfully completed.
SQL>
或者,为了显示的目的,在 SQL*Plus 中你可以使用这个:
SQL> var l_rec refcursor
SQL> exec p_test3(10, :l_rec);
PL/SQL procedure successfully completed.
SQL> print l_rec
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
7782 CLARK MANAGER 7839 09.06.1981 00:00:00 2450 10
7839 KING PRESIDENT 17.11.1981 00:00:00 5000 10
7934 MILLER CLERK 7782 23.01.1982 00:00:00 1300 10
SQL>