oracle--游标--bai
--复制表
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的更多相关文章
- Oracle 游标示例,带异常处理
Oracle游标示例一则,带异常处理. DECLARE CURSOR c_dl IS SELECT ID, NSRSBH, WSPZXH, ZXYY_DM, HZRQ, SWJG_DM, GXSJ F ...
- Oracle游标带参数
Oracle游标是可以带参数的,而SqlServer的游标就不可以了 create or replace procedure a as cursor b(c_id int)is select * fr ...
- Oracle 游标使用(转)
这个文档几乎包含了oracle游标使用的方方面面,全部通过了测试 ; ; dbms_output.put_line(sql) loop dbms_output.put_line( ; ; ; r_te ...
- Oracle 游标使用全解(转)
转自:http://www.cnblogs.com/sc-xx/archive/2011/12/03/2275084.html 这个文档几乎包含了oracle游标使用的方方面面,全部通过了测试 -- ...
- Oracle游标动态赋值
1. oracle游标动态赋值的小例子 -- 实现1:动态给游标赋值 -- 实现2:游标用表的rowtype声明,但数据却只配置表一行的某些字段时,遍历游标时需fetch into到精确字段 CREA ...
- dapper支持oracle游标
dapper支持oracle游标 Dapper是一个轻型的ORM类.它有啥优点.缺点相信很多朋友都知道了,园里也有很多朋友都有相关介绍,这里就不多废话. 如果玩过Oracle都知道,存储过程基本都是通 ...
- Oracle游标的使用示例
此文是使用Oracle游标的几种方式,for...in会自动打开游标,fetch...into需要手动打开游标,游标类似于一个只会往前移动的指针,每次指向数据集中的一行数据,通过游标可以打开数据集,也 ...
- Oracle游标介绍
Oracle游标使用详解: 游标: 用来查询数据库,获取记录集合(结果集)的指针,我们所说的游标通常是指显式游标,因此从现在起没有特别指明的情况,我们所说的游标都是指显式游标.要在程序中使用游标,必须 ...
- Oracle游标使用
Oracle游标介绍: --声明游标 CURSOR cursor_name IS select_statement --For 循环游标 --()定义游标 --()定义游标变量 --()使用for循环 ...
- [转载]Oracle 游标使用全解
这个文档几乎包含了oracle游标使用的方方面面,全部通过了测试 -- 声明游标:CURSOR cursor_name IS select_statement --For 循环游标--(1)定义游标- ...
随机推荐
- SQL通过ContentTypeID找使用了内容类型的列表库
DECLARE) SET @ContentTypeID='0x1B452DB25E92A34DA3E35FC8731278D2' SELECT w.Title AS [Web Site], w.Ful ...
- Android Weekly Notes Issue #232
Android Weekly Issue #232 November 20th, 2016 Android Weekly Issue #232 本期内容包括: Kotlin的优势讨论; MVVM模式结 ...
- Unable to download data from http://ruby.taobao.org/ & don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
安装cocoapods,记录两个问题! 1.镜像已经替换成了 http://ruby.taobao.org/, 还是不能不能安装cocoapods, 报错:Unable to download dat ...
- SpringMVC + mybatis + Druid insert 数据库中文乱码,查询无乱码
之前一直在pom文件配置的数据库连接url,很多配置都写在pom文件中导致配置文件太长 新项目将配置写到不同的文件夹中得properties文件中了 db.url直接复制的pom文件中的 p.p1 { ...
- 解析Jquery取得iframe中元素的几种方法
iframe在复合文档中经常用到,利用jquery操作iframe可以大幅提高效率,这里收集一些基本操作,需要的朋友可以参考下 DOM方法:父窗口操作IFRAME:window.frames[&q ...
- 机器学习实战笔记(Python实现)-06-AdaBoost
--------------------------------------------------------------------------------------- 本系列文章为<机器 ...
- log4j:ERROR Failed to rename [/log/xxx.log] to [/log/xxx.log.2016-11-23.log]
Log4j报错: log4j:ERROR Failed to rename [/log/xxx.log] to [/log/xxx.log.2016-11-23.log] google了一下发现是个b ...
- Android使用最小宽度限定符时最小宽度的计算
Android开发中最头疼的问题之一就是兼容不同尺寸和分辨率的设备.这里推荐一篇总结的比较完整的<Android开发:最全面.最易懂的Android屏幕适配解决方案>.这篇文章对屏幕兼容的 ...
- SVN提交代码的规范
协同开发中SVN的使用规范 先更新,再提交 SVN更新的原则是要随时更新,随时提交.当完成了一个小功能,能够通过编译并且自己测试之后,谨慎地提交. 如果在修改的期间别人也更改了svn的对应文件, ...
- MongoDB查询转对象是出错Element '_id' does not match any field or property of class
MongoDB查询转对象是出错Element '_id' does not match any field or property of class 解决方法: 1.在实体类加:[BsonIgno ...