游标

一定义

在oracle运行sql语句时会给sql语句分配一个缓冲区
游标是指向这个缓冲区的地址,可以通过游标获取到sql语句的执行结果
显示游标(sql语句()select语句)和隐式游标(dml语句)

二游标属性

1.%isopen 布尔类型变量,用来判断游标是否已经打开(可以在任意地方使用)
2.%found 布尔类型,用来判断当前游标是否有数据,有数据时返回true,没有数据时返回flase。
3.%notfound 只能在游标打开,并且执行了fetch into语句后才能使用
4.%rowcount 整数类型,可以表示游标中的行号,和数据总条数
(1)打开游标后 %found和%notfouund都不用,%rowcount的值是0
(2)执行fetch into语句,游标中有数据,%found=ture %notfound=flase
当取出游标的最后一条数据后,%fouond=flase %notfound=true
在取数据过程%rowcount 每取一条数据自动+1
(3)关闭游标之后,除%isopen之外,其他三个属性都不可用

三游标使用步骤:

1.打开游标
open 游标名
2.读取游标中的数据
fetch 游标 into 变量
3 关闭游标
close 游标
(1)显式游标
定义语法:

cursor 游标名称 is select语句;

loop循环游标


--遍历 使用loop循环来遍历游标
declare
   --声名一个游标
   cursor cur is select * from dept;
   --声名一个变量保存游标中的一条记录
   v cur%rowtype;
begin
   --打开游标
   open cur;
   --取游标数据,并打印
   loop
     --fetch into 从游标中取出数据 ,循环控制语句
     fetch cur into v;
     --退出循环语句
     exit when cur%notfound;
     --循环体语句,对游标中的数据进行处理
     dbms_output.put_line(cur%rowcount||','||v.deptno||','||v.dname||','||v.loc);
   
   end loop;
   --关闭游标
   close cur;
end;

while循环游标

declare
   --定义一个游标
   cursor cur is select * from dept;
   --声名一个变量保存游标中的一条记录
   v cur%rowtype;
begin
   --打开游标
   open cur;
   --执行fetch into 取游标中的第一条数据
   fetch cur into v;
   --while循环
   while cur%found loop
     --循环体语句
     dbms_output.put_line(cur%rowcount||','||v.deptno||','||v.dname||','||v.loc);
     --循环控制语句
     fetch cur into v;
   end loop;
   
   
   --关闭游标
   close cur;
end;

for 循环游标

declare
   --定义一个游标
   cursor cur is select * from dept;
begin
  for v in cur loop
    --循环体语句
    dbms_output.put_line(cur%rowcount||','||v.deptno||','||v.dname||','||v.loc);
  end loop;
end;

for循环遍历游标,不需要打开,关闭,不需要会执行fetvh ,这些都由for循环内部完成
缺点:出for循环,游标会自动关闭,关闭后不能使用%rowcount属性
2.带参数的游标
定义语法:

cursor 游标(形参,数据类型 [default 默认值] 形参 数据类型...is select 语句(使用到参数)

参数传递方式:

(1)传一个值
(2)传一个变量
(3)形参=>值

--根据部门编号查询部门下的员工,并打印员工信息
select * from emp where deptno=?;
declare
   --定义一个游标
   cursor cur(dno number) is select * from emp where deptno=dno;
   --声名一个变量保存游标的一条记录
   v cur%rowtype;
begin
   --打开游标,需要传递参数
   open cur(10);  ---表示查询20号部门的员工
   ---遍历
   loop
     --fetch into 
     fetch cur into v;
     --退出循环语句
     exit when cur%notfound;
     --循环体语句
     dbms_output.put_line(v.ename||','||v.job||','||v.mgr||','||v.hiredate||','||v.sal||','||v.comm||','||v.deptno);
   end loop;
   --关闭游标
   close cur;
end;
declare
  --定义一个游标
  cursor cur(dno number) is select * from emp where deptno=dno;
  --声名一个变量保存游标的一条记录
  v cur%rowtype;
  --声名一个变量接收键盘输入的一个部门编号
  v_deptno number:=&部门编号;
begin
  --打开游标
  open cur(v_deptno);
  --遍历
  fetch cur into v;
  while cur%found loop
    --循环体语句
    dbms_output.put_line(v.empno||','||v.ename||','||v.job||','||v.mgr||','||v.hiredate||','||v.sal||','||v.comm||','||v.deptno);
    --循环控制语句
    fetch cur into v;
  end loop;
  
  --关闭游标
  close cur;
end;

游标类型和游标变量

类型的定义语法:

type 类型名称 is ref cursoe

变量名 类型名

declare
   --定义一个游标类型
   type ctype is ref cursor;
   --声名变量
   cur ctype;
begin
  
end;

游标变量的使用

(1)打开游标
open 游标变量 for select 语句;
(2)遍历游标
fetch into
(3)关闭游标
close 游标变量

--写一个代码块,查询所有员工的姓名,并打印
select ename from emp;
declare
   --定义一个游标类型
   type ctype is ref cursor;
   --声名一个游标变量
   cur ctype;
   --声名一个变量保存游标中的一条记录
   v varchar2(30);
begin
   --打开游标
   open cur for select ename from emp;
   --loop循环遍历
   loop
     --fetch into语句
     fetch cur into v;
     --退出循环语句
     exit when cur%notfound;
     --循环体语句
     dbms_output.put_line(v);
   end loop;
   --关闭游标
   close cur;
end;

注意:游标变量只能使用loop或while循环遍历 ,不能使用for循环
系统游标类型
sys_refcursor :游标类型系统定义好的 type ctype is ref cursor
总结:
游标用来处理一个集合数据,针对集合数据进行不同处理时用(和for循环相同)
(4)隐式游标
隐式游标:在执行insert,update ,delete 语句时,数据库会给分配一个游标,名称叫sql%rowcount:取一个dml语句影响的数据库表数据条数

begin
  update emp set sal=sal+500;
  dbms_output.put_line(sql%rowcount);
end;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值