当前所在位置:珠峰网资料 >> 计算机 >> Oracle认证 >> 正文
综合辅导:oracle游标(二)
发布时间:2010/10/6 10:30:43 来源:www.xue.net 编辑:城市总裁吧
(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    布尔值 游标是否打开

  (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;

  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;

  (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;

广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved