oracle 游标相关资料
游标
概述:游标是系统为用户开设的一个数据缓冲区,存放 SQL 语句的执行结果。 我们可以把游标理解为 PL/SQL 中的结果集,把游标当中一个集合
1:在声明区声明游标
cursor 游标名称 is/as sql语句; 注意用is/as连接
2:基本语法
open 游标名称; --打开游标,须加分号
loop
fetch 游标名称 into xx; 从游标中抓取放在xx中
exit when 游标名称%notfound; 不能无限循环,抓取完了,必须退出
end loop;
close 游标名称
案例一:一般的游标的操作
--游标的操作
select * from t_pricetable where ownertypeid=1
--在声明区声明游标并带返回值
declare --必须创建declare声明区,声明游标必须带介词is/as
v_pricetable t_pricetable%rowtype; --带返回值参数,用来存储提取的游标
cursor cur_pricetable is select * from t_pricetable where ownertypeid=1;
begin
--开启游标
open cur_pricetable;
loop
fetch cur_pricetable into v_pricetable;
-- 游标抓取完后,自动退出
exit when cur_pricetable%notfound;
--执行打印语句
dbms_output.put_line( '价格:'||v_pricetable.price ||'吨位:'||v_pricetable.minnum||'-'||v_pricetable.maxnum );
end loop;
close cur_pricetable; --关闭游标
end;
案例二:带参游标
--带参数游标的操作
select * from t_pricetable where ownertypeid=1
--在声明区声明游标并带返回值
declare
v_pricetable t_pricetable%rowtype; --带返回值参数,用来存储提取的游标
--声明带参的游标(v_ownertype(游标的参数) number) 将ownertypeid(业主类型)直接使用游标的参数v_ownertype 就相当于索引被游标替换掉了
cursor cur_pricetable is select * from t_pricetable where ownertypeid=1;
begin
--开启游标
open cur_pricetable; --当带参的话,()中可以写业主类型
loop
fetch cur_pricetable into v_pricetable;
-- 游标抓取完后,自动退出
exit when cur_pricetable%notfound;
--执行打印语句
dbms_output.put_line( '价格:'||v_pricetable.price ||'吨位:'||v_pricetable.minnum||'-'||v_pricetable.maxnum );
end loop;
close cur_pricetable; --关闭游标
end;
案例三:循环提取游标值
declare
cursor cur_pricetable(v_ownertypeid number) is select *
from T_PRICETABLE where ownertypeid=v_ownertypeid;--定义游标
begin
for v_pricetable in cur_pricetable(3)
loop
dbms_output.put_line('价格:'||v_pricetable.price ||'吨位:'||v_pricetable.minnum||'-'||v_pricetable.maxnum );
end loop;
end ;
参数在游标定义时使用,打开时传入参数,例如:
create or replace procedure a
as
cursor b(c_id int)is select * from d where id=c_id;
begin
open b(111);
end;
参游标是指带有参数的游标。在定义了参数游标之后,当使用不同参数值多次打开游标时,可以生成不同的结果集。定义参数游标的语法如下:
CURSOR cursor_name(parameter_name datetype) IS select_statement;
注意,当定义参数游标时,游标参数只能指定数据类型,而不能指定长度。当定义参数游标时,一定要在游标子查询的where子句中引用该参数,否则就失去了定义参数游标的意义。
最新文章
- Java设计模式之单例模式
- OpenGL学习笔记0——安装库
- CentOS6.5下安装Open vSwitch
- hadoop集群配置实例
- Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值
- leetcode problem 37 -- Sudoku Solver
- Orchard 学习-手动安装Orchard
- css控制竖直文字显示
- Ansible快速上手
- va注解应用实例 - Annotation, 自定义注解, 注解类规则【转】
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习4
- C# 数组与集合的区别
- Django学习笔记之URL与视图cookie和session
- 摹客iDoc 新功能“柔性工作流”,让设计随需而动
- gitlab的ssh key有2个
- 本地代码上传到githup
- imp-oracle10g数据库dmp导入到11g数据库提示IMP-00058,表或试图不存在
- python爬虫---urllib库的基本用法
- [部署]VM11下CentOS7mini安装及配置
- [转帖学习]Howto Shrink a Thin Provisioned Virtual Disk (VMDK)