--复制表
create table emp as(select * from scott.emp);
select * from emp;
--(1) 最简单的游标
declare --声明并初始化游标
cursor v_cur is
select empno,ename from emp order by empno;
v_empno emp.empno%type;
v_ename emp.ename%type;
begin
if(not v_cur%isopen) then --打开游标
open v_cur;
end if;
loop --提取记录
fetch v_cur into v_empno,v_ename;
if(v_cur%notfound) then --到达末尾,则退出循环
exit;
end if;
dbms_output.put_line('第'||v_cur%rowcount||'条记录:'||v_empno||','||v_ename);
end loop;
close v_cur; --关闭游标
end;
--(2) 带参数的游标
--查找指定部门的所有员工
cursor v_cur(v_deptno number) is
select empno,ename from scott.emp where deptno = v_deptno order by empno ;
v_empno scott.emp.empno%type;
v_ename scott.emp.ename%type;
v_dno scott.emp.deptno%type;
begin
v_dno:='&请输入部门编号:';
if(not v_cur%isopen) then
open v_cur(v_dno);
end if;
dbms_output.put_line('部门号'||v_dno||'的员工如下:');
loop
fetch v_cur into v_empno,v_ename;
if(v_cur%notfound) then --到达末尾,则退出循环
exit ;
end if;
dbms_output.put_line('第'||v_cur%rowcount||'条记录:'||v_empno||','||v_ename);
end loop;
--关闭游标
close v_cur;
exception
when others then
dbms_output.put_line('提取出错');
end; --(3) 可写游标。让每每个员工工资增加1000元
declare
--声明并初始化游标
cursor v_cur is
select 1 from scott.emp order by empno
for update of sal nowait ;
--v_sal scott.emp.sal%type;
i number;
begin
--打开游标
if(not v_cur%isopen) then
open v_cur;
end if;
--提取记录
loop
fetch v_cur into i;
if(v_cur%notfound) then --到达末尾,则退出循环
exit ;
end if;
--update scott.emp set sal = sal+10000 where empno = v_empno;
update scott.emp set sal = sal+10000 where current of v_cur;
end loop;
dbms_output.put_line('成功修改了'||v_cur%rowcount||'人的工资');
--关闭游标
close v_cur;
end; --(4) 简化方式1
declare
--声明并初始化游标
cursor v_cur is
select empno,ename from scott.emp order by empno;
begin
--提取记录
for rec in v_cur loop
dbms_output.put_line('第'||v_cur%rowcount||'条记录:'||rec.empno
||','||rec.ename);
end loop; end; --(5) 简化方式2
declare
--声明并初始化游标
begin
--提取记录
for rec in (select empno,ename from scott.emp order by empno)
loop
dbms_output.put_line(rec.empno
||','||rec.ename);
end loop; end; --(6) 返回游标的函数
--返回周三入职的员工
select * from scott.emp where to_char(hiredate,'day')='星期五'; create or replace function get_emps_hired_oneday
(
v_day varchar2
)return sys_refcursor
as
v_cur sys_refcursor;--声明1个局部变量游标,用于返回
begin
--打开指定结果集的游标.
open v_cur for
select ename,hiredate from scott.emp
where to_char(hiredate,'day')=v_day;
return v_cur; --将该记录集的游标返回
end; --调用函数,浏览全部的记录。 declare
v_cur sys_refcursor;
v_ename varchar2(20);
v_hiredate date;
begin --调用别人写好函数。
v_cur:= get_emps_hired_oneday('星期五'); --提取记录
loop
fetch v_cur into v_ename,v_hiredate;
if(v_cur%notfound) then --到达末尾,则退出循环
exit ;
end if;
dbms_output.put_line('第'||v_cur%rowcount||'条记录:'||v_ename||','
||to_char(v_hiredate,'yyyy-mm-dd day'));
end loop;
--关闭游标
close v_cur; end;

  

oracle--游标--bai的更多相关文章

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

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

  2. Oracle游标带参数

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

  3. Oracle 游标使用(转)

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

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

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

  5. Oracle游标动态赋值

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

  6. dapper支持oracle游标

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

  7. Oracle游标的使用示例

    此文是使用Oracle游标的几种方式,for...in会自动打开游标,fetch...into需要手动打开游标,游标类似于一个只会往前移动的指针,每次指向数据集中的一行数据,通过游标可以打开数据集,也 ...

  8. Oracle游标介绍

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

  9. Oracle游标使用

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

  10. [转载]Oracle 游标使用全解

    这个文档几乎包含了oracle游标使用的方方面面,全部通过了测试 -- 声明游标:CURSOR cursor_name IS select_statement --For 循环游标--(1)定义游标- ...

随机推荐

  1. 读取数据库数据,并将数据整合成3D饼图在jsp中显示

    首先我将生成饼图的方法独立写成一个PieChar.java类,详细代码如下:(数据库需要自己建,如有需要的话) import java.io.IOException; import java.sql. ...

  2. 【代码笔记】iOS-自定义开关

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> #import "ToggleView.h&qu ...

  3. 生成freeswitch事件的几种方式

    本文描述了生成freeswitch事件的几种方式,这里记录下,也方便我以后查阅. 操作系统:debian8.5_x64 freeswitch 版本 : 1.6.8 在freeswitch代码中加入事件 ...

  4. MapReduce工作流多种实现方式

    学习 hadoop,必不可少的就是编写 MapReduce 程序.当然,对于简单的分析程序,我们只需一个 MapReduce 任务就能搞定,然而对于比较复杂的分析程序,我们可能需要多个Job或者多个M ...

  5. tftp-nfs开发环境搭建

    嵌入式开发通常使用主机-开发板的开发模式,在裸板开发中,我们通常使用串口调试工具传递文件,比如windows平台的超级终端,SecuCRT以及Linux平台的ckermit(题外话:ckermit比w ...

  6. 每天5分钟 玩转OpenStack 目录列表

    最近在学习 OpenStack 的相关知识,一直苦于 OpenStack 的体系庞大以及复杂程度,学习没有进度,停滞不前.偶然机会在 51CTO 上发现了一个热点的专题关于 OpenStack 的,题 ...

  7. RHEL6和RHEL7恢复root用户密码

    一.RHEL6恢复root密码 将系统重启,出现如下界面按上下键选择会停住,并输入e键 选中下图红框选项,再输入e键 再输入1,进入单用户模式 输入b进行启动   修改密码,然后重启 二.RHEL7恢 ...

  8. 由一个多线程共享Integer类变量问题引起的。。。

    最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...

  9. Linux线程基础

    复习中掌握线程的基本管理即可,而不用考虑线程的同步: 创建线程花费的代价,比创建进程小得多,所以同一个进程的,多个线程执行多个任务-->比多个进程执行多个任务更有效率. 线程也分为用户级线程.内 ...

  10. [LeetCode] Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...