Oracle笔记 九、PL/SQL 游标的使用
--演示隐式游标,系统自动声明,自动打开,自动使用并且自动关闭
begin
update emp set sal = 1000;
dbms_output.put_line('影响的行数:' || sql%rowcount);
end;
rollback;
/*游标的使用方法:
第一步:声明游标
第二步:打开游标
第三步:使用游标进行循环操作
第四步:关闭游标*/
--普通游标,游标本身就是一个变量
declare
--下面的这行代码声明了一个游标
cursor mycur is select * from emp where deptno = 20;
emprow emp%rowtype;
begin
open mycur; --打开游标
loop
fetch mycur into emprow; --把游标所指的纪录放到变量中
exit when (mycur%notfound); --当游标没有指向行时退出循环
dbms_output.put_line('名字:' || emprow.ename || '薪水:' || emprow.sal);
end loop;
close mycur; --关闭游标
end;
--简单游标,列操作
declare
empname emp.ename%type;
empsal emp.sal%type;
cursor mycur is select ename,sal from emp where deptno = 30;
begin
open mycur;
loop
fetch mycur into empname,empsal;
exit when mycur%notfound;
dbms_output.put_line('姓名:' || empname || '工资' || empsal);
end loop;
end;
--简单游标,列操作
declare
cursor c
is
select * from dept;
vDept_row_record c%rowtype;
begin
open c;
fetch c into vDept_row_record;
dbms_output.put_line(vDept_row_record.dname);
close c;
end;
--when循环游标
declare
cursor c
is
select * from dept;
vDept_row_record c%rowtype;
begin
open c;
loop
fetch c into vDept_row_record;
exit when(c%notfound);
dbms_output.put_line(vDept_row_record.dname);
end loop;
close c;
end;
--while循环游标
declare
cursor c
is
select * from dept;
vDept_row_record c%rowtype;
begin
open c;
fetch c into vDept_row_record;
while (c%found) loop
dbms_output.put_line(vDept_row_record.dname);
fetch c into vDept_row_record;
end loop;
close c;
end;
--for循环游标
declare
cursor c
is
select * from dept;
vDept_row_record c%rowtype;
begin
for vDept_row_record in c loop
dbms_output.put_line(vDept_row_record.dname);
end loop;
end;
--带参游标
declare
cursor c(sSal emp.sal%type, sEmpno emp.empno%type)
is
select * from emp where sal >= sSal and empno > sEmpno;
begin
for record_data in c(2500, 6666) loop
dbms_output.put_line(record_data.ename);
end loop;
end;
--update游标
declare
cursor c(sSal emp2.sal%type)
is
select * from emp2 where sal >= sSal for update;
begin
for record_data in c(2500) loop
if (record_data.sal < 3000) then
update emp2 set sal = sal + 3 where current of c;
dbms_output.put_line(record_data.ename);
elsif (record_data.sal = 5000) then
update emp2 set sal = sal - 3 where current of c;
dbms_output.put_line(record_data.ename);
end if;
end loop;
end;
--引用游标不能使用循环游标的语法
--引用游标不能进行删除和修改
--引用游标是一个数据类型,使用该类型必须声明变量
--弱类型引用游标,就是不指定游标将要提取的数据行的类型
declare
type my_cur_type is ref cursor;
mycur my_cur_type;--声明变量
which varchar2(10);
deptrow dept%rowtype;
emprow emp%rowtype;
begin
which := '&请选择dept还是emp';
if (which = 'dept') then
open mycur for select * from dept;
loop
fetch mycur into deptrow;
exit when (mycur%notfound);
dbms_output.put_line(deptrow.deptno || ' ' || deptrow.dname);
end loop;
elsif (which = 'emp') then
open mycur for select * from emp;
loop
fetch mycur into emprow;
exit when (mycur%notfound);
dbms_output.put_line(emprow.empno || ' ' || emprow.ename);
end loop;
end if;
close mycur;
end;
--强类型引用游标,就是指定游标将要提取的数据行的类型 ,只能是record或%rowtype类型
--比如:return number是错的,return emp.ename%type也是错的
declare
type mycurtype is ref cursor return emp%rowtype;
mycur mycurtype;--声明变量
emprow emp%rowtype;
begin
open mycur for select * from emp;
loop
fetch mycur into emprow;
exit when mycur%notfound;
dbms_output.put_line(emprow.empno || ' ' || emprow.ename);
end loop;
close mycur;
end;
Oracle笔记 九、PL/SQL 游标的使用的更多相关文章
- Oracle数据库之PL/SQL游标
1. 游标概念 字面意思是游动的光标,是指向上下文区域的句柄或指针. 在PL/SQL块中执行CRUD操作时,ORACLE会在内存中为其分配上下文区.用数据库语言来描述游标就是:映射在上下文区结果集中一 ...
- Oracle学习笔记之五,Oracle 11g的PL/SQL入门
1. PL/SQL概述 PL/SQL(Procedural Language/SQL)是Oracle的专用语言,是对标准SQL语言的扩展,它允许在其内部嵌套普通的SQL语句,还可以定义变量和常量,允许 ...
- oracle数据库之PL/SQL 块结构和组成元素
一.PL/SQL 块 (一)PL/SQL 程序由三个块组成,即声明部分.执行部分.异常处理部分 PL/SQL 块的结构如下: 1.DECLARE /* 声明部分: 在此声明 PL/SQL 用到的变量, ...
- 每周一书《Oracle 12 c PL(SQL)程序设计终极指南》
本周为大家送出的书是<Oracle 12 c PL(SQL)程序设计终极指南>,此书由机械工业出版社出版, 孙风栋,王澜,郭晓惠 著. 内容简介: <Oracle 12c PL/SQ ...
- Oracle数据库之PL/SQL包
Oracle数据库之PL/SQL包 1. 简介 包(PACKAGE)是一种数据对象,它是一组相关过程.函数.变量.常量和游标等PL/SQL程序设计元素的组合,作为一个完整的单元存储在数据库中,用名称来 ...
- Oracle数据库之PL/SQL异常处理
Oracle数据库之PL/SQL异常处理 异常指的是在程序运行过程中发生的异常事件,通常是由硬件问题或者程序设计问题所导致的. PL/SQL程序设计过程中,即使是写得最好的程序也可能会遇到错误或未预料 ...
- Oracle数据库之PL/SQL程序设计简介
PL/SQL程序设计简介 一.什么是PL/SQL? PL/SQL是 Procedure Language & Structured Query Language 的缩写. ORACLE的SQL ...
- Oracle11g R2学习系列 之九 PL/SQL语言
这是个重头戏,如果精通了PL/SQL,毫不夸张的说明精通了Oracle了.PL/SQL由以下三个部分组成(Definition,Manipulation,Control): DDL:数据定义语言,Cr ...
- PL/SQL 游标
本随笔不是原创,只是学习笔记,用于加深记忆,原创地址PL/SQL --> 游标 一.游标的相关概念和特性 1.定义: 映射到结果集中的某一行的特定位置,类似与C语言中的指针.即通过游标方式定位到 ...
- oracle系列(四)PL/SQL
过程,函数,触发器是PL/SQL编写的,存储在oracle中的.PL/SQL是非常强大的数据库过程语言. PL/SQL优点:性能,模块化,网络传输量,安全性缺点:移植性不好 简单分类:块:过程,函数, ...
随机推荐
- Shell_Shell调用SQLPlus简介(案例)
2014-06-20 Created By BaoXinjian
- python (11)文件的读写 按行读文件
读文件: 读取文件 f = open('\info.txt') fil = f.read() f.close() 按行读文件: f = open("info.txt") while ...
- StringIO 模块用于在内存缓冲区中读写数据
模块是用类编写的,只有一个StringIO类,所以它的可用方法都在类中.此类中的大部分函数都与对文件的操作方法类似. 例: #coding=gbk import StringIO s=StringIO ...
- android之location01
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <Linear ...
- Python深入01 特殊方法与多范式
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明. Python一切皆对象,但同时,Python还是一个多范式语言(multi-paradi ...
- ICE安装
第一步,基于Windows下的安装,所以下载windows版的Ice: http://www.zeroc.com/download 第二步,安装Ice: 常规安装即可,可以选择安装目录,本 ...
- java小程序实例 闰年
判断闰年. package com.test; import java.util.Scanner; import org.junit.Test; public class TestRunNian { ...
- crm 2011 plugin setparent setbusiness 用户更改经理 更改办事处
背景: 在更改经理或者更改办事处时,使用plugin处理相应的团队. 问题:plugin写完,注册时发现使用update注册没有效果,然后bing得到,这里要使用setbusiness 和 setpa ...
- 查看django里所有的url
>>> from django.core.urlresolvers import get_resolver >>> get_resolver(None).rever ...
- Ext TreeGrid提交修改过的数据
本打算将整个treestore的数据提交到服务器,但找来找去没有找到好的方法,在翻api的时候发现了getUpdatedRecords()方法,拿来一试,试出此方法可以拿到被修改过的record so ...