Oracle游标(光标)

# 游标(光标)cursor:集合

语法:
 

   declare
    cursor 光标名(参数列表)
  is 

# 光标的属性

  %isopen    %rowcount    %found   %notfound

--查询并打印全部员工的姓名、薪水
SET SERVEROUTPUT ON
declare
  cursor cemp is select ename,sal from emp;
  pename emp.ename%type;
  psal emp.sal%type;
begin
  open cemp;--打开光标
    loop --一行行循环
      fetch cemp into pename,psal; --获取当前行元素,移动到下一行
      exit when cemp%notfound;
      
      dbms_output.put_line(pename ||'的工资:'|| psal);
    end loop;
  close cemp;--关闭光标
end;
--涨工资,根据job类别涨工资,president涨1000,manager涨500,其他涨300
SET SERVEROUTPUT ON
declare 
  cursor cemp is select empno,job from emp;
  pempno emp.empno%type;
  psal emp.sal%type;
  pjob emp.job%type;
begin
  open cemp;
  loop
    fetch cemp into pempno,pjob;
    exit when cemp%notfound;
    if pjob = 'PRESIDENT' 
        then update emp set sal = sal+1000 where empno = pempno;
    elsif pjob = 'MANAGER'
        then update emp set sal = sal+500 where empno = pempno;
    else
        update emp set sal = sal+300 where empno = pempno;
    end if;
  end loop;
    dbms_output.put_line('ok');
  close cemp;
end;
原文地址:https://www.cnblogs.com/wzhqzm/p/13551453.html