加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

oracle的游标笔记

发布时间:2020-12-12 15:53:27 所属栏目:百科 来源:网络整理
导读:1. 游标 游标是一条SQL语句执行之后的结果状态信息 1.1 隐式游标 当执行DML语句时,会自动创建隐式游标,包含 %FOUND: 影响大于 0 条数据为true , %NOTFOUND:影响 0 条数据为true , %ROWCOUNT:影响数据的条数 , %ISOPEN: 是否打开,隐式游标始终为fal
1. 游标
   游标是一条SQL语句执行之后的结果状态信息
   1.1 隐式游标
       当执行DML语句时,会自动创建隐式游标,包含
       %FOUND: 影响大于0条数据为true ,
       %NOTFOUND:影响0条数据为true  ,
       %ROWCOUNT:影响数据的条数 ,
       %ISOPEN: 是否打开,隐式游标始终为false

       隐式游标的名称叫做SQL
       declare
           new_sal number;
           new_empno number;
       begin
           new_sal := &newsal;
           new_empno := &empno;
           -- 修改值
           update emp set sal = new_sal where empno = new_empno;
           -- 执行完DML时,会创建隐式游标

           if SQL%FOUND then
             dbms_output.put_line('更新成功,本次更新了' || SQL%ROWCOUNT || '条数据');
           else
              dbms_output.put_line('没有更新到数据。');
           end if;

           commit;
       end;

       -- 删除创建隐式游标
       begin
           delete from salgrade;

           if SQL%rowcount > 0 then
             dbms_output.put_line('删除了很多数据');
           end if;
         end;

   1.2 显示游标
       显示定义一个查询结果为游标
       1.2.1 在声明块中声明显式游标对象
       1.2.2 使用显式游标之前必须打开显示游标
             for循环迭代游标时会自动打开游标,所以不能手动打开

       1.2.3 通过fetch关键字将游标中的数据行抓取到指定行类型
       1.2.4 使用完游标,需要手动关闭
             for循环使用完游标会自动关闭,无需手动关闭

       declare
         -- 声明一个显示游标
         cursor my_cursor is select * from emp where deptno = 20;
         -- 表示emp表的行类型
         emp_row emp%rowtype;
       begin

         -- 显示游标必须先打开
         open my_cursor;

         loop
           -- 通过fetch关键字抓取出一行数据
           fetch my_cursor into emp_row; 
           -- 当游标中没有数据可取时退出循环
           exit when my_cursor%notfound;
           dbms_output.put_line('ename: ' || emp_row.ename || ',sal: ' || emp_row.sal);
         end loop;

         -- 使用完游标时需要关闭
         close my_cursor;
       end;


       /* for循环操作游标。 无需手动打开和关闭 无需声明for循环的临时变量 */
       declare
         -- 声明一个显示游标
         cursor my_cursor is select * from emp where deptno = 20;
       begin

         -- for循环会自动打开游标,无需手动打开
         for emp_row in my_cursor loop
           dbms_output.put_line('ename: ' || emp_row.ename || ',sal: ' || emp_row.sal);
         end loop;
       end;

   1.3 REF动态游标
       declare
         dno number;
         emp_row emp%rowtype;

         -- 首先声明游标类型
         type my_ref_cursor is ref cursor return emp%rowtype;

         -- 使用游标类型声明游标变量
         -- cur_emp1 表示游标变量,是my_ref_cursor类型的游标变量
         cur_emp1 my_ref_cursor;

       begin
         dno := &dno;

         -- 动态游标是在open时才动态指定查询sql
         open cur_emp1 for select * from emp where deptno = dno;

         fetch cur_emp1 into emp_row;
         -- 循环遍历游标中的数据
         while cur_emp1%found loop
              dbms_output.put_line('ename: ' || emp_row.ename || ',sal: ' || emp_row.sal);

              fetch cur_emp1 into emp_row;
         end loop;
         -- 关闭游标
         close cur_emp1;
       end;

       -- ** for循环不能作用于REF动态游标

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读