iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >Oracle中游标Cursor的用法详解
  • 379
分享到

Oracle中游标Cursor的用法详解

2024-04-02 19:04:59 379人浏览 泡泡鱼
摘要

目录一、使用游标1.定义游标2.打开游标3.提取数据4.关闭游标5.游标属性6.参数游标二、for循环遍历,实现遍历游标最高效方式。三、使用游标更新或删除数据四、通过bulk col

一、使用游标

对于DML语句和单行select into ,oracle自动分配隐形游标。处理select返回多行语句,可以使用显式游标。

使用显示游标处理多行数据,也可使用SELECT..BULK COLLECT INTO 语句处理多行数据.

1.定义游标

cursor cursor_name is select_statement;

2.打开游标

执行对应的SELECT语句并将SELECT语句的结果暂时存放到结果集中.

open cursor_name;

3.提取数据

打开游标后,SELECT语句的结果被临时存放到游标结果集中,使用FETCH语句只能提取一行数据

通过使用FETCH..BULK COLLECT INTO语句每次可以提取多行数据

fetch cursor_name into variable1,varibale2,...;

fetch cursor_name bulk collect into collect1,collect2,...[limit rows];

(1)游标中使用fetch..into语句:只能处理一行数据,除非用循环语句

declare
          v_bookname varchar2(100);
          cursor c_book(i_id number) is select bookname from book where id = i_id;
    begin
        Open c_book(10);--打开游标
        Loop
            Fetch c_book into v_bookname; --提取游标
            exit when c_book%notfound;
            update book set price = '33' where bookname = v_bookname;
        End Loop;
        Close c_book;--关闭游标
    end;

declare
          v_bookname varchar2(100);
          cursor c_book(i_id number) is select bookname from book where id = i_id;
begin
          Open c_book(10);
          Fetch c_book into v_bookname;--预先Fetch一次
          While c_book%found Loop
              update book set price = '33' where bookname = v_bookname;
               Fetch c_book into v_bookname;
          End Loop;
         Close c_book;
end;

(3)基于游标定义记录变量

declare
    cursor emp_cursor is select ename,sal from emp;
    emp_record emp_cursor%rowtype;
  begin
    open emp_cursor;
    loop
     fetch emp_cursor into emp_record;
     exit when emp_cursor%notfound;
     dbms_output.put_line('雇员名:'||emp_record.ename||',雇员工资:'||emp_record.sal);
    end loop;
 end;

4.关闭游标

close cursor_name;

5.游标属性

用于返回显示游标的执行信息,包括%isopen,%found,%notfound,%rowcount

  • %isopen:确定游标是否打开
  • %found:检查是否从结果集中提取到了数据
  • %notfound:与%found行为相反。
  • %rowcount:返回当前行为止已经提取到的实际行数

no_data_found和%notfound的用法是有区别的,小结如下1)SELECT. . . INTO 语句触发 no_data_found;
2)当一个显式光标(静态和动态)的 where 子句未找到时触发 %notfound;
3)当UPDATE或DELETE语句的where 子句未找到时触发 sql%notfound;
4)在光标的提取(Fetch)循环中要用 %notfound 或%found 来确定循环的退出条件,不要用no_data_found。

6.参数游标

注意:定义参数游标时,游标参数只能指定数据类型,而不能指定长度。

declare
    cursor emp_cursor(no number) is select ename from emp where deptno=no;
    v_ename emp.ename%type;
  begin
    open emp_cursor(10);
    loop
     fetch emp_cursor into v_ename;
     exit when emp_cursor%notfound;
     dbms_output.put_line(v_ename);
    end loop;
    close emp_cursor;
  end;

二、for循环遍历,实现遍历游标最高效方式。

使用FOR循环时,ORACLE会隐含的打开游标,提取游标数据并关闭游标。

每循环一次提取一次数据,在提取了所有数据后,自动退出循环并隐含的关闭游标。

1.使用游标FOR循环

--不需要声明v_bookname,Open和Close游标和fetch操作(不用打开游标和关闭游标,实现遍历游标最高效方式)
declare
 cursor c_book(i_id number) is select bookname from book where id = i_id;
begin
   for cur in c_book(10) loop --循环变量cur不需要声明
     update book set price = '53' where bookname = cur.bookname;
   end loop;
end;

2.在游标FOR循环中直接使用子查询

begin
     for emp_record in (select ename,sal from emp) loop
        dbms_output.put_line(emp_record.ename);
    end loop;
end;

三、使用游标更新或删除数据

要通过游标更新或删除数据,在定义游标时必须要带有FOR UPDATE子句

cursor cursor_name(parameter_name datetype) is select_statement for update [of column_reference] [nowait];
  • for update子句:用于在游标结果集数据上家行共享,防止其他用户在相应行执行DML操作
  • of子句:确定哪些表要加锁,没有OF子句,则在所引用的全部表上加锁
  • nowait子句:用于指定不等待锁
  • 必须在UPDATE后DELETE语句中引用WHERE CURRENT OF子句
    update table_name set column=.. where current of cursor_name;
    delete table_name where current of cursor_name;
declare
    cursor emp_cursor is select ename,sal from emp for update;
    v_ename emp.ename%type;
    v_sal emp.sal%tyep;
  begin
    open emp_cursor;
    loop
     fetch emp_cursor into v_ename,v_oldsal;
     exit when emp_cursor%notfound;
     if v_oldsal<2000 then
        update emp set sal=sal+100 where current of emp_cursor;--delete from emp where current of emp_cursor;
     end if;
   end loop;
   close emp_cursor;
 end;

四、通过bulk collect减少loop处理的开销

将查询结果一次性加载到集合中,而不是一条一条的加载。

(1)在显示游标中,使用FETCH..BALK COLLECT INTO语句提取所有数据

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

(2)游标中使用FETCH..BULK COLLECT INTO ..LIMIT语句提取部分数据

declare
    type name_array_type is varray(5) of varchar2(10);
    name_array name_array_type;
    cursor emp_cursor is select ename from emp;
    rows int:=5;
    v_count int:=0;
  begin
    open emp_cursor;
    loop
     fetch emp_cursor bulk collect into name_array limit rows;
     dbms_output.pur('雇员名');
     for i in 1..(emp_currsor%rowcount-v_count) loop
       dbms_output.put(name_array(i)||' ');
     end loop;
     dbms_output.new_line;
    v_count:=emp_cursor%rowcount;
    exit when emp_cursor%notfound;
    end loop;
    close emp_cursor;
  end;

五、使用游标变量

PL/SQL的游标变量中存放着指向内存地址的指针.

1.游标变量使用步骤

包括定义游标变量,打开游标,提取游标数据,关闭游标等四个阶段

1.1定义ref cursor类型和游标变量

type ref_type_name is ref cursor [return return_type];

cursor_varibale ref_type_name;

当指定RETURN子句时,其数据类型必须是记录类型,不能在包内定义游标变量

1.2打开游标

open cursor_variable for select_statement;

1.3提取游标数据

fetch cursor_varibale into variable1,variable2,...;

fetch cursor_varibale bulk collect into collect1,collect2,...[limit rows]

1.4关闭游标变量

close cursor_varibale;

2.游标变量使用示例

1、在定义FEF CURSOR类型时不指定RETURN子句

在打开游标时可以指定任何的SELECT语句

declare
    type emp_cursor_type is ref cursor;
    emp_cursor emp_cursor_type;
    emp_record emp%rowtype;
  begin
    open emp_cursor for select * from emp where deptno=10;
    loop
     fetch emp_cursor into emp_record;
     exit when emp_cursor%notfound;
     dbms_output.put_line('第'||emp_curosr%rowcount||'个雇员: '||emp_record.ename);
    end loop;
    close emp_cursor;
  end;

2、在定义REF CURSOR类型时指定RETURN子句

在打开游标时SELECT语句的返回结果必须与RETURN子句所指定的记录类型相匹配.

declare
    type emp_record_type is record(name varchar2(10),salary number(6,2));
    type emp_cursor_type is ref cursor return emp_record_type;
    emp_cursor emp_cursor_type;
    emp_record emp_record_type;
  begin
    open emp_cursor for select ename,sal from emp where deptno=20;
    loop
     fetch emp_cursor into emp_record;
     exit when emp_cursor%notfound;
     dbms_output.put_line('第'||emp_curosr%rowcount||'个雇员: '||emp_record.ename);
    end loop;
    close emp_cursor;
 end;

到此这篇关于Oracle中游标Cursor用法的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: Oracle中游标Cursor的用法详解

本文链接: https://www.lsjlt.com/news/148014.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Oracle中游标Cursor的用法详解
    目录一、使用游标1.定义游标2.打开游标3.提取数据4.关闭游标5.游标属性6.参数游标二、for循环遍历,实现遍历游标最高效方式。三、使用游标更新或删除数据四、通过bulk col...
    99+
    2024-04-02
  • Oracle中的游标Cursor怎么使用
    在Oracle中,游标(Cursor)是一种用于处理结果集的数据库对象。游标可以被用来遍历和操作查询结果集中的行。以下是使用游标的基...
    99+
    2023-08-15
    Oracle Cursor
  • 详解Oracle游标的简易用法
    下面看下Oracle游标的简易用法,具体代码如下所示: create or replace procedure NW_DelYW(iOPERATION_ID number, ...
    99+
    2024-04-02
  • Oracle游标使用详解
    Oracle游标是一种用于处理结果集的数据库对象。游标是一个私有的数据库对象,它可以存储查询结果集,并允许用户在结果集中进行操作。以...
    99+
    2023-09-15
    Oracle
  • oracle中游标的用法
    游标是一种遍历查询结果集的机制,让开发者逐行处理数据。其用法包括:1. 声明游标;2. 打开游标;3. 提取数据;4. 获取行状态;5. 关闭游标。游标的优点包括逐行处理、减少网络流量和...
    99+
    2024-05-09
    oracle
  • SQL中游标(cursor)的基本使用实例
    目录 类型:1.普通游标2.滚动游标具体FETCH用法:Arguments总结 类型:   1.普通游标   只有NEXT操作   2.滚动游标 有多种操作 1.普通游标 DEC...
    99+
    2024-04-02
  • Oracle中的游标和函数详解
     Oracle中的游标和函数详解 1.游标 游标是一种 PL/SQL 控制结构;可以对 SQL 语句的处理进行显示控制,便于对表的行数据 逐条进行处理。 游标并不是一个数据库对象,只是存留在内...
    99+
    2024-04-02
  • oracle中cursor的用法有哪些
    在Oracle中,游标(cursor)是一种数据库对象,用于处理查询结果集。以下是Oracle中使用游标的一些常见用法:1. 隐式游...
    99+
    2023-08-08
    oracle cursor
  • 详解Oracle隐式游标和显式游标
    游标是什么?就是在内存开辟的一块临时存储空间。 1.Oracle隐式游标 1.1Oracle有常用的哪些隐式游标 1.2 Oracle隐式游标演示 -- 隐式游标 (使用的表为Oracle默认自带的em...
    99+
    2024-04-02
  • oracle中游标的作用
    游标是 oracle 数据库中遍历结果集并逐行处理数据的机制,主要用于:遍历结果集处理大数据集逐行更新或删除数据事务处理 Oracle 中游标的作用 什么是游标? 游标是 Oracle...
    99+
    2024-05-09
    oracle
  • mysql和Oracle游标的用法
    本篇内容介绍了“mysql和Oracle游标的用法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!使用游标根...
    99+
    2024-04-02
  • Oracle中的游标
    目录 一、游标定义 二、游标分类 2.1静态游标 2.1.1. 隐式游标: 2.1.2. 显式游标: 2.1.3. 游标变量: 2.1.4. 游标参数: 2.2动态游标 2.2.1强类型游标 2.2.2弱类型游标 2.2.3普通动态游标 三...
    99+
    2023-09-21
    oracle 数据库 java
  • MySQL游标概念与用法详解
    本文实例讲述了MySQL游标概念与用法。分享给大家供大家参考,具体如下: 1、游标的概念(Cursor) 一条sql,对应N条资源,取出资源的接口,就是游标,沿着游标,可以一次取出1行。如果开发过安卓的同...
    99+
    2024-04-02
  • MyBatis游标Cursor的正确用法和百万数据传输的内存测试
    很早以前为了处理大量数据想过使用Cursor,当时发现没有效果,就没有继续深入。这次为了搞清楚 Cursor 是否真的有用,找些资料和源码发现是有效果的,只是缺了必要的配置。 准备测试数据 创建表: ...
    99+
    2023-09-01
    mybatis sql mysql 流式传输 百万数据
  • 通过Cursor工具使用GPT-4的方法详解
    目录下载Cursor Cursor1. 登录GitHub:Sign in to GitHub · GitHub2. 输入Cursor中给出的8位code&nb...
    99+
    2023-05-20
    Cursor 工具使用GPT-4 GPT-4使用
  • oracle中distinct的用法详解
    在Oracle中,DISTINCT关键字用于从查询结果中去除重复的行。它可以应用于SELECT语句中的一个或多个列,以确保查...
    99+
    2023-08-14
    oracle
  • Maven中resources标签的用法详解
    目录一、resources作用1、打包编译作用2、配置文件取pom当中的值二、演示resources打包编译的作用1、创建springboot项目2、进行编译打包3、出现问题4、得出...
    99+
    2024-04-02
  • CSS中的cursor属性实例用法
    本篇内容介绍了“CSS中的cursor属性实例用法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、开篇之...
    99+
    2024-04-02
  • MySQL游标语法的用法
    本篇内容主要讲解“MySQL游标语法的用法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“MySQL游标语法的用法”吧! 1、基本语法:  Sql代码  #定义游...
    99+
    2024-04-02
  • 怎么 理解ORACLE的游标共享
    这篇文章主要讲解了“怎么 理解ORACLE的游标共享”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么 理解ORACLE的游标共享”吧!游标共享(curso...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作