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)定义游标- ...
随机推荐
- 深化管理、提升IT的数据平台建设方案
谈到信息化,每个企业有每个企业的业务模式,每个企业有每个企业不同的思考.落地有效的信息化建设一定紧跟着企业的发展,围绕业务和管理,来提升效率,创造价值. 对于企业如何在发展的不同阶段提升信息化建设,这 ...
- [Modern OpenGL系列(三)]用OpenGL绘制一个三角形
本文已同步发表在CSDN:http://blog.csdn.net/wenxin2011/article/details/51347008 在上一篇文章中已经介绍了OpenGL窗口的创建.本文接着说如 ...
- 用户代理字符串识别工具源码与slf4j日志使用
User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本.CPU 类型.浏览器及版本.浏览器渲染引擎.浏览器语言.浏览器插件等.UA也成为了, ...
- JuCheap V2.0响应式后台管理系统模板正式发布beta版本
JuCheap V1.* 查看地址: http://blog.csdn.net/allenwdj/article/details/49155339 经过半年的努力,JuCheap后台通用响应式管理后台 ...
- java知识总结(更新中)
一.java 数据类型 基本类型(byte.short.int. long. char.float.double.boolean) 数字类型 整数型:byte(8).short(16).int(32) ...
- 用application实现一个网页的浏览计数器
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- Linux下面安装MySQL
- linux输入子系统(input subsystem)之按键输入和LED控制
实验现象:在控制台打印按键值,并且通过按键控制相应的LED亮灭. 1.代码 input_subsys_drv.c #include <linux/module.h> #include &l ...
- 通过中看不中用的代码分析Ioc容器,依赖注入....
/** * 通过生产拥有超能力的超人实例 来理解IOC容器 */ //超能力模组接口 interface SuperModuleInterface{ public function activate( ...
- android EditText 默认情况下不获取焦点(不弹出输入框)
可以在EditText前面放置一个看不到的LinearLayout,让它率先获取焦点: <LinearLayout android:focusable="true" andr ...