设为首页 收藏本站
查看: 3030|回复: 0

[经验分享] ORACLE游标实例讲解

[复制链接]

尚未签到

发表于 2018-9-12 10:17:16 | 显示全部楼层 |阅读模式
  1.当在PL/SQL块中执行查询语句SELECT和数据操纵语句DML时,ORACLE会为其分配上下文区(CONTEXTAREA),游标指上下文区指针
  对于数据操纵语句和单行SELECTINTO语句来说,ORACLE会为他们分配隐含游标.
  使用显示游标处理多行数据,也可使用SELECT..BULK COLLECT INTO语句处理多行数据.
  1.显示游标
  定义游标
  cursorcursor_name is select_statement;
  2.打开游标:执行对应的SELECT语句并将SELECT语句的结果暂时存放到结果集中.
  opencursor_name;
  3.提取数据
  打开游标后,SELECT语句的结果被临时存放到游标结果集中,使用FETCH语句只能提取一行数据
  通过使用FETCH..BULK COLLECT INTO语句每次可以提取多行数据
  fetchcursor_name into variable1,varibale2,...;
  fetchcursor_name bulk collect into collect1,collect2,...[limitrows];
  4.关闭游标
  closecursor_name;
  9.2显示游标属性
  用于返回显示游标的执行信息,包括%isopen,%found,%notfound,%rowcount
  1.%isopen:确定游标是否打开if cl%isopen then ... else  open c1; end if;
  2.%found:检查是否从结果集中提取到了数据
  loop
  fetch c1 into var1,var2;
  if c2%found then ... else exit;
  endloop;
  3.%notfound
  loop
  fetch c1 into var1,var2;
  exit when c2%notfound;
  ...
  endloop;
  4.%rowcount:返回当前行为止已经提取到的实际行数
  loop
  fetch c1 into my_ename,my_deptno;
  if c1%rowcount>10 then
  ...
  end if;
  ...
  endloop;
  9.3显示游标使用示例
  1.在显示游标中使用fetch..into语句:只能处理一行数据,除非用循环语句
  declare
  cursoremp_cursor is select ename,sal from emp where deptno=10;
  v_enameemp.ename%type;
  v_salemp.sal%type;
  begin
  openemp_cursor;
  loop
  fetch emp_cursor into v_ename,v_sal;
  exit when emp_cursor%notfound;
  dbms_output.put_line(v_ename||': '||v_sal);
  end loop;
  close emp_cursor;
  end;
  2.在显示游标中,使用FETCH..BALK COLLECT INTO语句提取所有数据
  declare
  cursor emp_cursor is selectename from emp where deptno=10;
  typeename_table_type is table of varchar2(10);
  ename_tableename_table_type;
  begin
  openemp_cursor;
  fetchemp_cursor bulk collect into ename_table;
  for i in1..ename_table.count loop
  dbms_output.put_line(ename_table(i));
  endloop;
  closeemp_cursor;
  end;
  3.在显示游标中使用FETCH..BULK COLLECT INTO ..LIMIT语句提取部分数据
  declare
  typename_array_type is varray(5) of varchar2(10);
  name_arrayname_array_type;
  cursoremp_cursor is select ename from emp;
  rowsint:=5;
  v_countint:=0;
  begin
  openemp_cursor;
  loop
  fetch emp_cursor bulk collect into name_array limit rows;
  dbms_output.pur('雇员名');
  for i in 1..(emp_currsor%rowcount-v_count) loop
  dbms_output.put(name_array(i)||' ');
  end loop;
  dbms_output.new_line;
  v_count:=emp_cursor%rowcount;
  exit when emp_cursor%notfound;
  endloop;
  closeemp_cursor;
  end;
  4.使用游标属性
  declare
  cursoremp_cursor is select ename from emp where deptno=10;
  typeename_table_type is table ofvarchar2(10);
  ename_tableename_table_type;
  begin
  if notemp_cursor%isopen then
  open emp_cursor;
  end if;
  fetchemp_cursor bulk collect into ename_table;
  dbms_output.put_line('提取的总计行数:'||emp_cursor%rowcount);
  closeemp_cursor;
  end;
  5.基于游标定义记录变量
  declare
  cursoremp_cursor is select ename,sal from emp;
  emp_recordemp_cursor%rowtype;
  begin
  openemp_cursor;
  loop
  fetch emp_cursor into emp_record;
  exit when emp_cursor%notfound;
  dbms_output.put_line('雇员名:'||emp_record.ename||',雇员工资:'||emp_record.sal);
  endloop;
  end;
  9.4参数游标
  定义参数游标时,游标参数只能指定数据类型,而不能指定长度.
  cursorcursor_name(parameter_name datatype) is select_statment;
  declare
  cursoremp_cursor(no number) is select ename from emp where deptno=no;
  v_enameemp.ename%type;
  begin
  openemp_cursor(10);
  loop
  fetch emp_cursor into v_ename;
  exit when emp_cursor%notfound;
  dbms_output.put_line(v_ename);
  endloop;
  closeemp_cursor;
  end;
  9.5使用游标更新或删除数据
  要通过游标更新或删除数据,在定义游标时必须要带有FOR UPDATE子句
  cursorcursor_name(parameter_name datetype) is select_statement for update[of column_reference] [nowait];
  forupdate子句用于在游标结果集数据上家行共享锁,防止其他用户在相应行执行DML操作
  of子句确定哪些表要加锁,没有OF子句,则在所引用的全部表上加锁
  nowait子句用于指定不等待锁
  必须在UPDATE后DELETE语句中引用WHERE CURRENT OF子句
  update table_nameset column=.. where current of cursor_name;
  delete table_namewhere current of cursor_name;
  1.使用游标更新数据
  declare
  cursoremp_cursor is select ename,sal from emp for update;
  v_enameemp.ename%type;
  v_salemp.sal%tyep;
  begin
  openemp_cursor;
  loop
  fetch emp_cursor into v_ename,v_oldsal;
  exit when emp_cursor%notfound;
  if v_oldsal

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-573227-1-1.html 上篇帖子: Oracle DG 之逻辑备库--Switchover 下篇帖子: Linux下设置Oracle开机自动启动
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表