oracle 游标的含义,三类游标的使用方式,fetch...bulk collect into批量提取数据

1.1 定义

  当执行查询语句时,对于表中的记录,是通过逐行处理的形式查询结果
在这里插入图片描述

在这里插入图片描述


1.2 游标类型

  隐式游标、显示游标、REF游标

1.2.1 隐式游标:

  在PL/SQL程序中执行DML SQL语句时自动创建隐式游标,名字固定叫sql;隐式游标自动声明、打开、关闭;通过检查隐式游标的属性可以获取最近执行的DML语句的信息;

隐式游标的属性有:
  sql%ROWCOUNT 受最近的SQL语句影响的行数
  sql%FOUND 最近的SQL语句是否影响了一行及以上的数据
  sql%NOTFOUND 最近的SQL语句是否未影响任何数据
  sql%ISOPEN 对于隐式游标而言永远为FALSE

如:

set serverout on ;
declare
	begin
	update emp set JOB='mmm' where SAL=1250;
	dbms_output.put_line('更新了'||sql%ROWCOUNT||'行');
	end;
	/

在这里插入图片描述

declare
	begin
	update emp set JOB='mmm' where SAL=1250;
	if sql%FOUND then
	dbms_output.put_line('更新了'||sql%ROWCOUNT||'行');
	else
	dbms_output.put_line('没有更新');
	end if;
	end;
	/

在这里插入图片描述

1.2.2 显示游标:用于处理返回多行的查询

  在PL/SQL块的声明部分定义查询,该查询可返回多行;

显示游标的操作过程:
  声明游标、打开游标、使用游标取出记录、关闭游标
在这里插入图片描述

如:
PL/SQL错误地从scott的emp表中取出所有记录:

declare
	sname varchar(20);
	begin 
	select ENAME into sname from scott.emp;
	dbms_output.put_line(sname );
	exception when too_many_rows then
		dbms_output.put_line('取出数据多余一行');
	end;
	/

在这里插入图片描述
  emp1 emp%rowtype 定义为emp中行类型
通过显示游标逐行读取:

declare
	emp1 emp%rowtype;
	cursor mycursor is select * from scott.emp;
	begin 
	open mycursor;
	fetch mycursor into emp1;
	while mycursor%FOUND loop
	dbms_output.put_line(emp1.ENAME ||'薪水:'||emp1.SAL);
	fetch mycursor into emp1;
	end loop;
	close mycursor ;
	 end ;
	 /

在这里插入图片描述

带参数游标:
员工id大于输入id的员工信息

declare
	EMPNO1 emp.EMPNO%type;
	emp1 emp%rowtype;
	cursor mycursor(input_num number) is select * from scott.emp where empno>input_num ;
	begin 
	EMPNO1 := &员工id ;
	open mycursor(EMPNO1 );
	fetch mycursor into emp1;
	while mycursor%FOUND loop
	dbms_output.put_line(emp1.ENAME ||'薪水:'||emp1.SAL);
	fetch mycursor into emp1;
	end loop;
	close mycursor ;
	 end ;
	 /

在这里插入图片描述

允许使用游标删除或更新活动集中的行,声明游标必须使用 select ... for update

更新删除操作需要添加 where current of 游标

declare
	emp1 emp%rowtype;
	cursor mycursor is select * from scott.emp e where  e.empno=5223 or  e.empno=7369 for update;
	begin 
	open mycursor;
	fetch mycursor into emp1;
	while mycursor%FOUND loop
	update emp set JOB='uouo' where current of mycursor ;
	fetch mycursor into emp1;
	end loop;
	close mycursor ;
	 end ;
	 /

在这里插入图片描述

1.2.3 循环游标

用于简化游标处理代码;
当用于需要从游标中提取所有记录时使用
仅供select时使用

declare
	cursor mycursor is select * from scott.emp;
	begin 
	for cur_2 in mycursor  loop
	dbms_output.put_line(cur_2 .ENAME ||'薪水:'||cur_2 .SAL);
	end loop;
	 end ;
	 /

在这里插入图片描述

1.2.4 fetch…bulk collect into

使用fetch bulk collect into 能有效提高读取游标速度:

declare 
	cursor my_cursor is select ename from emp where deptno=30;
	type ename_table_type is table of varchar2(10);
	ename_table ename_table_type ;
	begin
	open my_cursor ;
	fetch my_cursor bulk collect into ename_table ;
	for i in 1..ename_table.count loop
	dbms_output.put_line(ename_table(i));
	end loop;
	close my_cursor ;
	end;
	/

在这里插入图片描述

1.2.5 REF游标:

用于处理运行时才能确定的动态sql查询的结果;

创建游标变量需要2个步骤:声明REF游标类型,声明REF游标类型的变量

声明REF游标类型的语法:

type <类型名> is REF CURSOR [return <返回类型>];

打开游标变量:

open 游标名 for 查询语句;
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

但行益事莫问前程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值