我想创建一个在 Oracle 中使用游标作为输入的过程。我尝试过这样的事情:
cursor cur is select * from table t1;
然后还有:
create or replace procedure (I want to input cursor cur here)
is
begin
end;/
我该怎么做?
这是一个示例,展示了一种方法。
接受光标作为参数、循环遍历并显示其内容的过程:
SQL> create or replace procedure p_test (par_cursor in sys_refcursor)
2 is
3 l_ename emp.ename%type;
4 l_sal emp.sal%type;
5 begin
6 loop
7 exit when par_cursor%notfound;
8 fetch par_cursor into l_ename, l_sal;
9 dbms_output.put_line(l_ename ||' - '|| l_sal);
10 end loop;
11 end;
12 /
Procedure created.
如何使用?
SQL> set serveroutput on
SQL> declare
2 l_rc sys_refcursor;
3 begin
4 open l_rc for
5 select ename, sal
6 from emp
7 where deptno = 10;
8 p_test (l_rc);
9 end;
10 /
CLARK - 2450
KING - 5000
MILLER - 1300
MILLER - 1300
PL/SQL procedure successfully completed.
SQL>