【oracle资料整理】--【14】游标

1。 隐式游标
     单条sql语句所产生的结果集合
       用关键字SQL表示隐式游标
        4个属性 %rowcount  影响的记录的行数  整数
                %found     影响到了记录 true
                %notfound  没有影响到记录 true
                %isopen    是否打开  布尔值 永远是false

         多条sql语句 隐式游标SQL永远指的是最后一条sql语句的结果
         主要使用在update 和 delete语句上     

   2。 显式游标
      select语句上 使用显式游标
      明确能访问结果集
         for循环游标
         参数游标
           解决多行记录的查询问题
         fetch游标

显示游标
   需要明确定义
   (1)FOR循环游标 (常用的一种游标)

 --<1>定义游标
 --<2>定义游标变量
 --<3>使用for循环来使用这个游标

  --前向游标 只能往一个方向走
  --效率很高

      declare
        --类型定义
        cursor cc is select empno,ename,job,sal
         from emp where job = 'MANAGER';
        --定义一个游标变量
        ccrec cc%rowtype;


      begin
         --for循环
         for ccrec in cc loop
            dbms_output.put_line(ccrec.empno||'-'||ccrec.ename||'-'||ccrec.job||'-'||ccrec.sal);

         end loop;
      
      end;
   (2) fetch游标
     --使用的时候 必须要明确的打开和关闭

      declare
        --类型定义
        cursor cc is select empno,ename,job,sal
         from emp where job = 'MANAGER';
        --定义一个游标变量
        ccrec cc%rowtype;


      begin
        --打开游标
         open cc;
        --loop循环
         loop
            --提取一行数据到ccrec中
            fetch cc into ccrec;        
            --判断是否提取到值,没取到值就退出
            --取到值cc%notfound 是false
            --取不到值cc%notfound 是true
            exit when cc%notfound;
            dbms_output.put_line(ccrec.empno||'-'||ccrec.ename||'-'||ccrec.job||'-'||ccrec.sal);           

         end loop;
        --关闭
         close cc; 
      
      end;
  游标的属性4种
       %notfound  fetch是否提到数据 没有true 提到false
       %found      fetch是否提到数据 有true 没提到false
       %rowcount  已经取出的记录的条数
       %isopen    布尔值 游标是否打开

 declare
        --类型定义
        cursor cc is select empno,ename,job,sal
         from emp where job = 'MANAGER';
        --定义一个游标变量
        ccrec cc%rowtype;
  

      begin
        --打开游标
         open cc;
        --loop循环
         loop
            --提取一行数据到ccrec中
            fetch cc into ccrec;        
            --判断是否提取到值,没取到值就退出
            --取到值cc%notfound 是false
            --取不到值cc%notfound 是true
            exit when (cc%notfound or cc%rowcount =3);

            dbms_output.put_line(cc%rowcount||'-'||ccrec.empno||'-'||ccrec.ename||'-'||ccrec.job||'-'||ccrec.sal);           

         end loop;
        --关闭
         close cc; 
      
      end;

<例子>
  declare
       cursor cc is select dept.dname,
        emp.ename,emp.sal from
         dept,emp where dept.deptno = emp.deptno;
       ccrec cc%rowtype;
  begin
      for ccrec in cc loop
     
       dbms_output.put_line(ccrec.dname||'-'||ccrec.ename||'-'||ccrec.sal);
      end loop;
  

  end; 
   (3)参数游标
 按部门编号的顺序输出部门经理的名字
     declare
       --部门
       cursor c1 is select deptno from dept;
       --参数游标c2,定义参数的时候
       --只能指定类型,不能指定长度 
       --参数只能出现在select语句=号的右侧
       cursor c2(no number,pjob varchar2) is select emp.* from emp
         where deptno = no and job=pjob;

    /*
       no = 10  pjob = 'MANAGER'
          select * from emp where deptno = 10 and job = 'MANAGER';  
     */
       c1rec c1%rowtype;
       c2rec c2%rowtype;
       --定义变量的时候要指定长度
       v_job varchar2(20);
     begin
       --部门
        for c1rec in c1 loop
          --参数在游标中使用
          for c2rec in c2(c1rec.deptno,'MANAGER') loop
            dbms_output.put_line(c1rec.deptno||'-'||c2rec.ename);

          end loop;
        end loop;
     end;
<综合例子>
  求购买的商品包括了顾客"Dennis"所购买商品的顾客(姓名);**************
   思路:
      Dennis (A,B)

      别的顾客 (A,B,C) (A,C) (B,C)  C
  declare
   --Dennis所购买的商品
   cursor cdennis is select productid
         from purcase where customerid=(
           select customerid from
           customer where name = 'Dennis');
   --除Dennis以外的每个顾客
   cursor ccust is select customerid
        from customer where name <> 'Dennis';
   --每个顾客购买的商品
   cursor cprod(id varchar2) is
     select productid from purcase
          where customerid = id;

   j number ;
   i number;
   c1rec cdennis%rowtype;
   c2rec ccust%rowtype;
   c3rec cprod%rowtype;
   cname varchar2(10);
  begin
    --顾客循环
    for c2rec in ccust loop
     i:=0;
     j:=0;
     for c1rec in cdennis loop
        i := i + 1;
       
      --每个顾客买的东西
       for c3rec in cprod(c2rec.customerid) loop

           if (c3rec.productid = c1rec.productid) then
               j := j + 1;
           end if;

        end loop;  
     
     end loop;
                       
       if (i=j) then
        select name into cname from
           customer where customerid = c2rec.customerid;
        DBMS_output.put_line(cname);       
       end if;

    end loop;

  end;
  (4)引用游标/动态游标
        select语句是动态的
     declare
       --定义一个类型(ref cursor)弱类型   
       type cur is ref cursor;
         --强类型(返回的结果集有要求)
       type cur1 is ref cursor return emp%rowtype;
       --定义一个ref cursor类型的变量  
       cura 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; 
       close cura;
    end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值