什么是参数化游标,在哪种情况下需要使用它?

问题描述 投票:0回答:1

我想知道属于PLSQL的参数化游标,那么什么是参数化游标...1)。什么时候以及如何使用此游标类型?2)。从哪种角度来看,我们可以使用参数化游标,即何时需要使用它?

plsql oracle11g
1个回答
0
投票

这是使用参数的游标。

[在必要时使用;如果游标不需要参数,则不要使用它(反之亦然)。可能的用途之一是嵌套的游标循环。基于Scott的模式,我首先获取部门(在游标FOR循环中),然后循环遍历属于该部门的所有员工,并作为参数传递:

SQL>   declare
  2    cursor c1 (par_deptno in number) is --> PAR_DEPTNO is cursor's parameter
  3      select ename
  4      from emp
  5      where deptno = par_deptno;
  6    c1r c1%rowtype;
  7  begin
  8    for cur_d in (select deptno, dname from dept) loop
  9      dbms_output.put_line('Dept. ' || cur_d.dname);
 10      dbms_output.put_line('  List of Employees:');
 11
 12      open c1 (cur_d.deptno);           --> when opening the cursor, pass parameter's value
 13      loop
 14        fetch c1 into c1r;
 15        exit when c1%notfound;
 16
 17        dbms_output.put_line('  - ' || c1r.ename);
 18      end loop;
 19      close c1;
 20    end loop;
 21  end;
 22  /
Dept. ACCOUNTING
List of Employees:
- CLARK
- KING
- MILLER
Dept. RESEARCH
List of Employees:
- SMITH
- JONES
- SCOTT
- ADAMS
- FORD
Dept. SALES
List of Employees:
- ALLEN
- WARD
- MARTIN
- BLAKE
- TURNER
- JAMES
Dept. OPERATIONS
List of Employees:

PL/SQL procedure successfully completed.

SQL>

文档中的更多信息(Explicit cursor)。

© www.soinside.com 2019 - 2024. All rights reserved.