oracle 的游标(cursor )的用法。

游标
游标分为显示游标和隐示游标。下面有两个实例然码实践这两个例子
cursor c_name(var_name var_type)
is|as
select ...
from ...<--游标的语法->
where...

游标的属性:
%isopen<--是否打开->
%found
%notfound
%rowcount

用法:
open
fetch
close

create or replace function f_get_ename(p_deptno number)
return number is
 s_sum number ;
 tem number ;
 --显示游标
 cursor c1(p_salary hr.employees.salary%type)
  is 
  select emp.salary
  from hr.employees emp
  where emp.employee_id > p_deptno and emp.employee_id < 205;
  
begin
  s_sum := 0 ;
  --打开游标
  open c1(202);
  loop
    --取数据
    fetch c1 into tem;
    exit when c1%notfound;
    s_sum := s_sum + tem ;
    
  end loop;
  dbms_output.put_line(c1%rowcount) ;
  close c1;
  
  return s_sum ;
end;

--隐式游标
create or replace function f_get_ename(p_deptno number) return number is
  s_sum number;

begin
  s_sum := 0;
  --隐式游标
  for c1 in (select emp.salary
               from hr.employees emp
              where emp.employee_id > p_deptno
                and emp.employee_id < 205) loop
    s_sum := s_sum + c1.salary;
  end loop;

  return s_sum;
end;

 

你可能感兴趣的:(oracle,C++,c,C#,F#)