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优点:性能,模块化,网络传输量,安全性缺点:移植性不好 简单分类:块:过程,函数, ...
随机推荐
- GL_Oracle Erp常用的报表(汇总)
2014-06-27 BaoXinjian 1. 总账系统
- POJ 3368 Frequent values RMQ 训练指南 好题
#include<cstdio> #include<cstring> ; const int inf=0x3f3f3f3f; inline int max(int x,int ...
- bug_ _小心android-support-v4.jar版本混乱造成的NoClassDefFoundError
当你的项目出现以下红色提示的时候,要小心了, 因为很可能因为这个错误而导致解释不通的异常出现. Found 2 versions of android-support-v4.jar in the de ...
- ylbtech-Unitity-CS:Delegates
ylbtech-Unitity-CS:Delegates 1.A,效果图返回顶部 Invoking delegate a: Hello, A! Invoking delegate b: Goodbye ...
- JAVA 继承 extends
/* 继承 1.提高了代码的复用性,简化了代码 2.让类与类之间产生了继承关系,才有了后面的多态的特性的存在 注意:千万不要为了获取其它类的功能简化代码,而建立继承关系, 必须要类与类之间存在继承关系 ...
- JAVA 数组算法(复制、查找、插入)
一.复制数组算法 //数组复制算法 public class Test{ public static void main(String[] args){ int[] arrA = {100,800,5 ...
- android之AutoCompleteTextView控件用法
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...
- NLP学习资源
Journals ACM Transactions on Information Systems (TOIS) 影响因子 5.059(2006) IEEE Transactions on Knowl ...
- .net常用组件
计划任务组件: Quartz.NETHangfireFluentSchedulerhttp://www.cnblogs.com/Irving/p/4053462.html 队列:rabbitMQ 长连 ...
- delphi的tserversocket控件如何接收16进制数
http://bbs.csdn.net/topics/390473005 对方客户端发送数据如:68 00 00··········:接收完成后,数据长度没错(13),但是显示接收结果时,只显示一个字 ...