oracle学习笔记10:游标

1.什么是游标

在oracle中执行select、update、insert、delete等操作时,oracle会在内存中为其分配上下文,即开辟一小块数据缓冲区,用于将查询到的结果集缓存起来。游标就是指向这个缓冲区的指针,便于进行逐行定位处理。游标使用完成后要及时关闭。

2.游标的分类

  显式游标:显示游标指定游标的名字,调用方式:游标名+属性

  隐式游标:没有名字,执行select、update、delete、insert操作时,会自动触发隐士游标,调用方式:sql+属性

3.游标的属性

  游标名%found     判断游标中是否读到数据

  游标名%notfound  判断游标中是否没有读到数据

  游标名%isopen   判断游标是否打开

  游标名%rowcount  获取游标中实际读取的行的数量

游标处理数据的效率较低,非必要情况,不建议使用

显示游标:

1.创建游标

declare
  --1. 定义游标
  -- cursor 游标名 is 查询命令
  cursor cur_emp is select empno,ename,job
                    from emp
                    where empno=7369;
  row1 emp.empno%type;
  row2 emp.ename%type;
  row3 emp.job%type;
begin
  --2. 打开游标   open 游标名
  open cur_emp;
  --3. 提取数据   fetch 游标名 into 变量1,变量2,...
  fetch cur_emp into row1,row2,row3;
  dbms_output.put_line(row1);
  dbms_output.put_line(row2);
  dbms_output.put_line(row3);
  --4. 关闭游标 close 游标名
  close cur_emp;
end;
declare
  cursor emp_cur is select *
                    from emp;
  row1 emp%rowtype;
begin
  --   当游标中有多个数据时,使用循环来提取数据
  open emp_cur;
    loop
      fetch emp_cur into row1;
      exit when emp_cur%notfound;
      dbms_output.put_line(emp_cur%rowcount||'--'||row1.empno||':'||row1.ename);
    end loop;
  close emp_cur;
end;
2.带参数的游标
-- 带参数的游标
declare
--   定义带参数的游标,在命令中使用参数
  cursor cur_emp(no emp.empno%type) is select * from emp where empno=no;
  row1 emp%rowtype;
  begin
--   打开游标传参
  open cur_emp(7369);
--   提取数据
  fetch cur_emp into row1;
  dbms_output.put_line(row1.ename||row1.job);
--   关闭游标
  close cur_emp;
end;
3.读取数据到表格中
-- 将数据读到表格中
declare
--   定义表格类型
  type emp_table is table of emp%rowtype index by binary_integer ;
--   声明表格变量
  v_emp emp_table;
--   声明游标
  cursor cur_emp is select * from emp;
  begin
--   打开游标
  open cur_emp;
--   提取游标内数据到表格,使用bulk collect into 关键字,批量插入到表格中
  fetch cur_emp bulk collect into v_emp;
--   使用循环来读取表中数据
  for i in v_emp.first..v_emp.LAST loop
    dbms_output.put_line(v_emp(i).ename||':'||v_emp(i).job);
  end loop;
end;
4.游标类型的变量
-- 游标类型的变量,存储过程中当作参数使用
create or replace procedure pro4(
cur sys_refcursor
)as
  row1 emp%rowtype;
begin
  loop
    fetch cur into row1;
    exit when cur%notfound;
    dbms_output.put_line(row1.ename);
  end loop;
end;
declare
  type cur_type is ref cursor ;
  emp_cur cur_type;
  begin
  open emp_cur for select * from emp;
  pro4(emp_cur);
  close emp_cur;
end;
5.使用for简化游标的操作
declare
  cursor emp_cur is select * from emp;
  begin
  for i in emp_cur loop
    dbms_output.put_line(i.ename);
  end loop;
end;

隐式游标(sql+属性名):

declare
  begin
  update emp set ename = 'lisi' where empno=7369;
  if sql%found then
    dbms_output.put_line('更新了数据');
  end if;
end;

系统游标类型

-- sys refcursor 系统游标变量
create or replace procedure pro5(cur out sys_refcursor)
as
begin
  open cur for select * from emp;
end;

declare
  cur  sys_refcursor;
  row1 emp%rowtype;
begin
  pro5(cur);
  loop
--     在存储过程中,给系统游标赋值,在这里直接就可以调用不用再次赋值
    fetch cur into row1;
    exit when cur%notfound;
    dbms_output.put_line(row1.ename);
  end loop;
  close cur;
end;

 

转载于:https://www.cnblogs.com/Zs-book1/p/11234204.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值