游标

概述:游标是系统为用户开设的一个数据缓冲区,存放 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子句中引用该参数,否则就失去了定义参数游标的意义。

最新文章

  1. Java设计模式之单例模式
  2. OpenGL学习笔记0——安装库
  3. CentOS6.5下安装Open vSwitch
  4. hadoop集群配置实例
  5. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值
  6. leetcode problem 37 -- Sudoku Solver
  7. Orchard 学习-手动安装Orchard
  8. css控制竖直文字显示
  9. Ansible快速上手
  10. va注解应用实例 - Annotation, 自定义注解, 注解类规则【转】
  11. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习4
  12. C# 数组与集合的区别
  13. Django学习笔记之URL与视图cookie和session
  14. 摹客iDoc 新功能“柔性工作流”,让设计随需而动
  15. gitlab的ssh key有2个
  16. 本地代码上传到githup
  17. imp-oracle10g数据库dmp导入到11g数据库提示IMP-00058,表或试图不存在
  18. python爬虫---urllib库的基本用法
  19. [部署]VM11下CentOS7mini安装及配置
  20. [转帖学习]Howto Shrink a Thin Provisioned Virtual Disk (VMDK)

热门文章

  1. day81:luffy:课程分类页面&课程信息页面&指定分类显示课程信息&分页显示课程信息
  2. 适合 C++ 新手学习的开源项目——在 GitHub 学编程
  3. DM的SQL优化入门笔记
  4. Matlab批量绘制图像并保存
  5. Mongoose Guide(转)
  6. SpringBoot第十一集:整合Swagger3.0与RESTful接口整合返回值(2020最新最易懂)
  7. Ceph部署的时候修改默认权重
  8. JS简单介绍与简单的基本语法
  9. 使用大乌龟git和码云搭建版本库
  10. git设置个人信息