Oracle 显式游标

-- 查找员工号,姓名,职位
declare
  -- 定义游标
  cursor mycur is select empno,ename,job from emp;
  v_empno emp.empno%type;
  v_ename emp.ename%type;
  v_job emp.job%type;
begin
  -- 打开游标
  open mycur;
  -- 提取数据
  loop
       fetch mycur into v_empno,v_ename,v_job;
       dbms_output.put_line('员工编号:'||v_empno||'员工名称:'||v_ename||'员工职位:'||v_job);
       -- 什么时候能够退出循环
       -- exit when mycur%notfound; 
       -- exit when not mycur%found;
       exit when mycur%rowcount=5;
  end loop;
  -- 关闭游标
  close mycur;
  exception 
    when others then
      dbms_output.put_line('出错'||sqlerrm);
end;

-- 检测游标是否打开
declare
  cursor mycur is
    select empno, ename, job from emp;
  v_empno emp.empno%type;
  v_ename emp.ename%type;
  v_job   emp.job%type;
begin
  open mycur;
  -- 检测游标是否打开
  if mycur%isopen then
    dbms_output.put_line('游标已经打开');
  else
    dbms_output.put_line('游标没有打开');
  end if;
  close mycur;
exception
  when others then
    dbms_output.put_line('出错' || sqlerrm);
end;

-- 按员工的职称涨工资,总裁涨1000元,经理涨500元,其他员工涨300元
declare
   cursor mycur is select empno,job from empnew;
   v_empno empnew.empno%type;
   v_job empnew.job%type;
begin
  open mycur;
  loop
    fetch mycur into v_empno,v_job;
      if v_job='董事长' then
        update empnew set sal = sal + 1000 where empno = v_empno;
      elsif v_job='经理' then
        update empnew set sal = sal + 500 where empno = v_empno;
      else 
        update empnew set sal = sal + 300 where empno = v_empno;
      end if;
      exit when mycur%notfound;
  end loop;
  commit;
  close mycur;
  exception
    when others then
      rollback;
      dbms_output.put_line('出错:'||sqlerrm);
end;

select * from empnew;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值