此文是使用Oracle游标的几种方式,for...in会自动打开游标,fetch...into需要手动打开游标,游标类似于一个只会往前移动的指针,每次指向数据集中的一行数据,通过游标可以打开数据集,也能用于遍历数据集中的数据,在存储过程中可以实现loop循环,以及一些比较复杂的逻辑,也可以用于在存储过程中返回一个查询到的程序集。

create or replace procedure my_loop_cur_pro(presult out varchar) as
/*方法一,for in*/
/* pstring varchar(500);
begin*/
/*for in 是隐式打开关闭游标的,所以不需要定义一个游标*/
/* for pitem in (select * from emp) loop
begin
if pitem.DEPTNO = 20 then
pstring := pstring || ',' || pitem.ENAME;
end if;
end;
end loop;
presult:=pstring;
end;*/
 
/*方法二*/
/*使用 fetch into 的方式,这种方式必须手动的打开和关闭游标,同时必须在loop的时候明确的定义退出的条件,否则会出现死循环的现象*/
/* Cursor pcursor is
select * from emp;
pitem emp%rowtype; --%rowtype用于行类型%type用于列,字段,属性(eg emp.depno%type)
begin
open pcursor;
loop
fetch pcursor
into pitem;
exit when pcursor%notfound;--定义完成游标时退出
if pitem.Deptno = 20 then
presult := presult || pitem.ename;
end if;
end loop;
close pcursor;--必须手动关闭游标
end;*/
 
/*方法三*/
/*使用While遍历游标*/
/*cursor pcursor is
select * from emp;
pitem emp%rowtype;
begin
open pcursor;
fetch pcursor
into pitem; --while循环的时候,必须先Fetch一次游标,否则不会允许游标
while pcursor%found loop
--定义当游标获取到数据的时候循环
fetch pcursor
into pitem;
if pitem.deptno = 20 then
presult := presult || pitem.ename;
else
if pitem.deptno <> 20 then
presult := presult || ',';
end if;
end if;
end loop;
close pcursor;
end;
*/

Oracle游标的使用示例的更多相关文章

  1. Oracle 游标示例,带异常处理

    Oracle游标示例一则,带异常处理. DECLARE CURSOR c_dl IS SELECT ID, NSRSBH, WSPZXH, ZXYY_DM, HZRQ, SWJG_DM, GXSJ F ...

  2. Oracle与Mysql内嵌游标的使用示例

    Oracle 游标用For循环比较简单,Mysql也是最近才开始用,感觉稍微麻烦一点,下边直接上代码: ------------------------------------------------ ...

  3. Oracle游标带参数

    Oracle游标是可以带参数的,而SqlServer的游标就不可以了 create or replace procedure a as cursor b(c_id int)is select * fr ...

  4. Oracle 游标使用(转)

    这个文档几乎包含了oracle游标使用的方方面面,全部通过了测试 ; ; dbms_output.put_line(sql) loop dbms_output.put_line( ; ; ; r_te ...

  5. Oracle 游标使用全解(转)

    转自:http://www.cnblogs.com/sc-xx/archive/2011/12/03/2275084.html 这个文档几乎包含了oracle游标使用的方方面面,全部通过了测试 -- ...

  6. Oracle游标动态赋值

    1. oracle游标动态赋值的小例子 -- 实现1:动态给游标赋值 -- 实现2:游标用表的rowtype声明,但数据却只配置表一行的某些字段时,遍历游标时需fetch into到精确字段 CREA ...

  7. dapper支持oracle游标

    dapper支持oracle游标 Dapper是一个轻型的ORM类.它有啥优点.缺点相信很多朋友都知道了,园里也有很多朋友都有相关介绍,这里就不多废话. 如果玩过Oracle都知道,存储过程基本都是通 ...

  8. Oracle游标介绍

    Oracle游标使用详解: 游标: 用来查询数据库,获取记录集合(结果集)的指针,我们所说的游标通常是指显式游标,因此从现在起没有特别指明的情况,我们所说的游标都是指显式游标.要在程序中使用游标,必须 ...

  9. Oracle游标使用

    Oracle游标介绍: --声明游标 CURSOR cursor_name IS select_statement --For 循环游标 --()定义游标 --()定义游标变量 --()使用for循环 ...

随机推荐

  1. sql server创建登录出发器后导致登录失败--解决方案

    1.选择sql server配置管理器---sql server服务--右键属性--启动参数--添加-f.-m两个参数并重启sql server服务 2.重新启动sql server以windos身份 ...

  2. [LeetCode] Keys and Rooms 钥匙与房间

    There are N rooms and you start in room 0.  Each room has a distinct number in 0, 1, 2, ..., N-1, an ...

  3. sv时序组合和时序逻辑

    input a; input b; input c; reg d; wire e; reg f; // 时序逻辑,有寄存器 always@(posedge clk)begin 'b1)begin d ...

  4. Mesos源码分析(7): Mesos-Slave的启动

      Mesos-Slave的启动是从src/slave/main.cpp中的main函数开始的.   看过了Mesos-Master的启动过程,Mesos-Slave的启动没有那么复杂了.   1. ...

  5. C#单元测试,带你快速入门

    注:本文示例环境 VS2017 XUnit 2.2.0 单元测试框架 xunit.runner.visualstudio 2.2.0 测试运行工具 Moq 4.7.10 模拟框架 为什么要编写单元测试 ...

  6. PostgreSQL+PostGIS 的使用

    一.PostGIS中的几何类型 PostGIS支持所有OGC规范的“Simple Features”类型,同时在此基础上扩展了对3DZ.3DM.4D坐标的支持. 1. OGC的WKB和WKT格式 OG ...

  7. 高性能网络服务器编程:为什么linux下epoll是最好,Netty要比NIO.2好?

    基本的IO编程过程(包括网络IO和文件IO)是,打开文件描述符(windows是handler,java是stream或channel),多路捕获(Multiplexe,即select和poll和ep ...

  8. Hive如何处理小文件问题?

    一.小文件是如何产生的 1.动态分区插入数据,产生大量的小文件,从而导致map数量剧增. 2.reduce数量越多,小文件也越多(reduce的个数和输出文件是对应的). 3.数据源本身就包含大量的小 ...

  9. [Swift]LeetCode546. 移除盒子 | Remove Boxes

    Given several boxes with different colors represented by different positive numbers. You may experie ...

  10. [Swift]LeetCode695. 岛屿的最大面积 | Max Area of Island

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...