oracle 游标练习

--地定义异常
declare
       myException exception;
       v_emp emp%rowtype;
begin
     select * into v_emp
            from emp
                 where empno=7369;
     if v_emp.sal<1000 then
        raise myException;
     end if;
     dbms_output.put_line('正常');
exception
         when myException then
              dbms_output.put_line('自定义异常');
              update emp s
                     set sal=1500
                        where empno=7369;
         when no_data_found then
              dbms_output.put_line('无数据');
         when others then
              dbms_output.put_line('其他异常');
end;
------------------------------------------------------
--游标
declare
       my_emp emp%rowtype;
       cursor my_cur is
              select *
                     from emp
                          where deptno=30;
begin
     open my_cur;
     fetch my_cur into my_emp;
     dbms_output.put_line(my_emp.empno||'   '||my_emp.ename);
     close my_cur;
end;

-----------------------------------------------
--for循环游标
declare
       my_emp emp%rowtype;
       cursor my_cur is
              select *
                     from emp
                          where sal>1000;
begin
     for my_emp in my_cur loop
         dbms_output.put_line(my_emp.empno||'   '||my_emp.ename);
     end loop; 
end;

--
declare
       my_emp emp%rowtype;
       cursor my_cur is
              select *
                     from emp
                          where sal>1000;
begin
     open my_cur;
     loop
         fetch  my_cur into my_emp
         exit when my_cur%notfound;
         dbms_output.put_line(my_emp.empno||'   '||my_emp.ename);
     end loop;
     close my_cur;
end;

----------------------------------------------------------------
--带参数游标
declare
       cursor c1 is select deptno from dept;
       cursor c2(nn number)is select * from emp where deptno=10;
       c1rec c1%rowtype;
       --c2rec c2%rowtype;
begin
     for cclrec in c1 loop
         dbms_output.put_line(cclrec.deptno);
         for cc2rec in c2(clrec.deptno) loop
             dbms_output.put_line(cc2rec.empno||'   '||cc2rec.ename||'    '||c2rec.deptno);
         end loop;
     end loop;
end;
--------------------------------------------------------------------
--ref游标
declare
       type ref_cur is ref cursor;
       --type curl is ref cursor return emp%rowtype;
       cura ref_cur;
       c1rec emp%rowtype;
       c2rec dept%rowtype;
begin
     dbms_output.put_line('输出员工');
     open cura for select * from emp;
     loop
          fetch cura into c1rec;
          exit when cura%notfound;
          dbms_output.put_line(c1rec.ename);
     end loop;
     dbms_output.put_line('输出部门');
     open cura for select * from dept;
     loop
          fetch cura into c2rec;
          exit when cura%notfound;
          dbms_output.put_line(c2rec.dname);
     end loop;   
end;

declare
       type ref_cur is ref cursor;
       type curl is ref cursor return emp%rowtype;
       cura curl;
       c1rec emp%rowtype;
       c2rec dept%rowtype;
begin
     dbms_output.put_line('输出员工');
     open cura for select * from emp;
     loop
          fetch cura into c1rec;
          exit when cura%notfound;
          dbms_output.put_line(c1rec.ename);
     end loop;      
end;
       

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值