--复制表
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. SharePoint 2016 配置向导报错 - The 'ListInternal' attribute is not allowed

    前言 配置SharePoint 2016的配置向导中,第三步创建配置数据库报错,然后百度.谷歌了一下,都没有解决,自己看日志搞定,也许会有人遇到类似问题,分享一下. 1.配置向导的错误截图,如下图: ...

  2. [tableView reloadData] 和 runloop

    需要[tableView reloadData]后需要立即获取tableview的cell.高度,或者需要滚动tableview,那么,直接在reloadData后执行代码是会有问题的. 断点调试感觉 ...

  3. 有主线程发送message给子线程

    通常我们在处理耗时任务时候都会通过新建线程来处理,当任务处理完后通过Handler将结果发送回主线程.比如下面示例: package com.example.testlistener; import ...

  4. UI篇(初识君面)

    我们的APP要想吸引用户,就要把UI(脸蛋)搞漂亮一点.毕竟好的外貌是增进人际关系的第一步,我们程序员看到一个APP时,第一眼就是看这个软件的功能,不去关心界面是否漂亮,看到好的程序会说"我 ...

  5. 一个高级的J2E工程师需要面对MySQL要有那些基本功夫呢<上>

    1. MySQL的架构介绍1.1 MySQL简介: MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,将数据保存在不 ...

  6. ThinkPHP实现定时任务

    项目服务端框架我选用的是ThinkPHP,由于策划案中有需求要定时刷新指定数据,所以在windows平台我使用微软的计划任务调用bat脚本来执行下面的命令来完成 php index.php /Home ...

  7. ubuntu系统安装初始化脚本

    ubuntu系统安装初始化脚本 经常安装卸载ubuntu,每次系统安装完成后要安装好多软件,一个个命令输很麻烦浪费时间,一个sh文件全搞定! 1. flashplayer下载:点击这里 2. Auda ...

  8. 《Note --- Unreal 4 --- B project --- Second UV issue》

    Second uv 可以通过editor来生成: 这部分内容都是在staticMeshEditor这个文件夹下面的代码里: 关于UI的相应机制,有个文件UICommandList.cpp例如我点击st ...

  9. 安全测试 - CSRF攻击及防御

    CSRF(Cross-site request forgery跨站请求伪造) 尽管听起来像跨站脚本(XSS),但它与XSS非常不同,并且攻击方式几乎相左.XSS利用站点内的信任用户,而CSRF则通过伪 ...

  10. C++使用vector

    #include <iostream> #include <string> #include <vector> using namespace std; void ...