Oracle Cursor详解与实例

php中文网
发布: 2016-06-07 17:35:34
原创
1572人浏览过

游标是SQL的一个内存工作区,由系统或用户以变量的形式定义。游标的作用就是用于临时存储从数据库中提取的数据块。在某些情况下,

摘要:详细介绍oracle数据库中关于游标的定义和使用。通过实例操作来深入了解cursor的用法和用处。

一:相关概念  1、concept 

When Oracle Database executes aSQL statement , it stores the result set and processing information in anunnamed private SQL area . A pointer to this unnamed area , called a cursor ,let you retrieve the rows of the result set one at a time . Cursor attributesreturn information about the state of the cursor . 

2、概念: 

 

二:具体类型及使用  1、implicit cursor 

3)示例:

begin
  update student set sname='chy' WHERE sno='1';
  if sql%isopen then
    dbms_output.put_line('cursor is opening !');
  else
    dbms_output.put_line('cursor is closed !');
  end if;
  if sql%found then
    dbms_output.put_line('DML is successed !');
  else
    dbms_output.put_line('DML is failed !');
  end if;
  if sql%notfound then
    dbms_output.put_line('DML is failed !');
  else
    dbms_output.put_line('DML is successed !');
  end if;
      dbms_output.put_line(sql%rowcount||' is the number of result !');
  exception
      when no_data_found then
          dbms_output.put_line('Sorry No data');
      when too_many_rows then
          dbms_output.put_line('Too Many rows');
end;

2explicit cursor

 

很直白的说明了显示游标的用处、以及用法。

] IS select xxx from xxxwhere xxx;

] ;

 

a)使用显示游标

v、关闭游标:将游标放入缓冲池中,没有完全释放资源。可重新打开。

close 游标名;

b)遍历循环游标

循环游标隐式打开游标,,自动滚动获取一条记录,并自动创建临时记录类型变量存储记录。处理完后自动关闭游标。

……

loop

eSiteGroup站群管理系统1.0.4
eSiteGroup站群管理系统1.0.4

eSiteGroup站群管理系统是基于eFramework低代码开发平台构建,是一款高度灵活、可扩展的智能化站群管理解决方案,全面支持SQL Server、SQLite、MySQL、Oracle等主流数据库,适配企业级高并发、轻量级本地化、云端分布式等多种部署场景。通过可视化建模与模块化设计,系统可实现多站点的快速搭建、跨平台协同管理及数据智能分析,满足政府、企业、教育机构等组织对多站点统一管控的

eSiteGroup站群管理系统1.0.4 0
查看详情 eSiteGroup站群管理系统1.0.4

数据处理语句;

end loop

……

loop

end loop

……

open 游标名

-- do something

 

end loop;

……

close 游标名


 

declare
  cursor cur is select * from t_user where age = 22;
  userinfo t_user%rowtype;
begin
  for userinfo in cur loop
    exit when cur%notfound;
    dbms_output.put_line('user id : ' || userinfo.id || '-' || 'user name : ' || userinfo.username);
  end loop;
  exception
    when others then
      dbms_output.put_line(sqlerrm);
end;     

ii

declare
  cursor cur is select * from t_user where age = 22;
  userinfo t_user%rowtype;
begin
  open cur;
  loop
    exit when cur%notfound;
    fetch cur into userinfo;
    dbms_output.put_line('user id : ' || userinfo.id || '-' || 'user name : ' || userinfo.username);
  end loop;
  exception
    when others then
          dbms_output.put_line(sqlerrm);
 close cur;
end;

linux

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号