Oracle中的游标的原理和使用详解

时间:2023-03-09 09:32:00
Oracle中的游标的原理和使用详解

游标的简介

Oracle中的游标的原理和使用详解

逐行处理查询结果,以编程的方式访问数据.

游标的类型:

1,隐式游标:在 PL/SQL 程序中执行DML SQL 语句时自动创建隐式游标,名字固定叫sql。

2,显式游标:显式游标用于处理返回多行的查询。

3,REF 游标:REF 游标用于处理运行时才能确定的动态 SQL 查询的结果

一、隐式游标:


q在PL/SQL中使用DML语句时自动创建隐式游标 q隐式游标自动声明、打开和关闭,其名为 SQL q通过检查隐式游标的属性可以获得最近执行的DML 语句的信息 q隐式游标的属性有: q%FOUND – SQL 语句影响了一行或多行时为 TRUE q%NOTFOUND – SQL 语句没有影响任何行时为TRUE q%ROWCOUNT – SQL 语句影响的行数 q%ISOPEN - 游标是否打开,始终为FALSE

begin
   ;
  if sql %FOUND   then
    dbms_output.put_line('这次更新了' || sql% rowcount );
    else
      dbms_output.put_line ('一行也没有更新' );
      end if;
      end;

在select中有两个中比较常见的异常: 1. NO_DATA_FOUND 2. TOO_MANY_ROWS

DECLARE
   sname1   student.sname%TYPE;
BEGIN
   SELECT sname INTO sname1 FROM student;

   IF SQL%FOUND
   THEN
      DBMS_OUTPUT.put_line (SQL%ROWCOUNT);
   ELSE
      DBMS_OUTPUT.put_line ('没有找到数据');
   END IF;
EXCEPTION
   WHEN TOO_MANY_ROWS
   THEN
      DBMS_OUTPUT.put_line ('查找的行记录多于1行');
   WHEN NO_DATA_FOUND
   THEN
      DBMS_OUTPUT.put_line ('未找到匹配的行');
END;

二、显式游标:

Oracle中的游标的原理和使用详解

sqlserver与oracle的不同之处在于: 最后sqlserver会deallocate 丢弃游标,而oracle只有前面四步: 声明游标、打开游标、使用游标读取记录、关闭游标。
显式游标的使用:

------------------------------------无参数游标-------------------------------
DECLARE
   sname   );                                               --声明变量

   CURSOR student_cursor
   IS
      SELECT sname FROM student;                                        --声明游标
BEGIN
   OPEN student_cursor;                                                 --打开游标

   FETCH student_cursor INTO sname;                                --让游标指针往下移动

   WHILE student_cursor%FOUND                                 --判断游标指针是否指向某行记录
   LOOP                                                                   --遍历
      DBMS_OUTPUT.put_line ('学生姓名' || sname);

      FETCH student_cursor INTO sname;
   END LOOP;

   CLOSE student_cursor;
END;
------------------------------------有参数游标-------------------------------
DECLARE
   sname   student.sname%TYPE;
   sno     student.sno%TYPE;

   CURSOR student_cursor (input_sno NUMBER)
   IS
      SELECT s.sname, s.sno
        FROM student s
       WHERE s.sno > input_sno;                                     --声明带参数的游标
BEGIN
   sno := &请输入学号;                           --要求从客户端输入参数值,"&"相当于占位符;

   OPEN student_cursor (sno);                                    --打开游标,并且传递参数

   FETCH student_cursor INTO sname, sno;                                --移动游标

   WHILE student_cursor%FOUND
   LOOP
      DBMS_OUTPUT.put_line ('学号为:' || sno || '姓名为:' || sname);

      FETCH student_cursor INTO sname, sno;
   END LOOP;

   CLOSE student_cursor;
END;
------------------------------------循环游标-------------------------------

DECLARE
   stu1   student%ROWTYPE;                             --这里也不需要定义变量来接收fetch到的值

   CURSOR student_cursor
   IS
      SELECT * FROM student;
BEGIN
   OPEN student_cursor;                                            --这里不需要开启游标

   FOR stu1 IN student_cursor
   LOOP
      DBMS_OUTPUT.put_line (
         '学生学号:' || stu1.sno || '学生姓名:' || stu1.sname);

      FETCH student_cursor INTO stu1;                             --也不需要fetch了
   END LOOP;

   CLOSE student_cursor;                                          --这里也不需要关闭游标
END;
------------------------------------使用游标更新行-------------------------------
DECLARE
   stu1   student%ROWTYPE;
   CURSOR student_cursor
   IS
      SELECT *
        FROM student s
       , )
      FOR UPDATE;                                                     --创建更新游标
BEGIN
   OPEN student_cursor;

   FETCH student_cursor INTO stu1;                                      --移动游标

   WHILE student_cursor%FOUND                                 --遍历游标,判断是否指向某个值
   LOOP
      UPDATE student

       WHERE CURRENT OF student_cursor;                         --通过游标中的信息更新数据

      FETCH student_cursor INTO stu1;                                   --移动游标
   END LOOP;

   CLOSE student_cursor;
END;

DECLARE
   stu1   student%ROWTYPE;

   CURSOR student_cursor
   IS
      SELECT *
        FROM student s
       , )
      FOR UPDATE;                                                     --创建更新游标
BEGIN
   OPEN student_cursor;

   -- fetch student_cursor into stu1;--移动游标
   -- while student_cursor%found--遍历游标,判断是否指向某个值
   LOOP
      FETCH student_cursor INTO stu1;                                   --移动游标

      EXIT WHEN student_cursor%NOTFOUND;

      UPDATE student

       WHERE CURRENT OF student_cursor;                         --通过游标中的信息更新数据
   END LOOP;

   CLOSE student_cursor;
END;
------------------------------------使用fetch ... bulk collect into-------------------------------
DECLARE
   CURSOR my_cursor
   IS
      SELECT ename
        FROM emp
       ;                                               --声明游标

   TYPE ename_table_type ); --定义一种表类型,表中的属性列为varchar2类型

   ename_table   ename_table_type;                            --通过上面定义的类型来定义变量
BEGIN
   OPEN my_cursor;                                                      --打开游标

   FETCH my_cursor BULK COLLECT INTO ename_table;                       --移动游标

    .. ename_table.COUNT
   LOOP
      DBMS_OUTPUT.put_line (ename_table (i));
   END LOOP;

   CLOSE my_cursor;
END;
DECLARE
   stu1   student%ROWTYPE;
   add1   address%ROWTYPE;

   CURSOR student_cursor
   IS
      SELECT *
        FROM student
      FOR UPDATE;                                                     --声明更新游标

   CURSOR address_cursor
   IS
      SELECT * FROM address;                                            --声明游标
BEGIN
   OPEN student_cursor;                                                 --打开游标

   FETCH student_cursor INTO stu1;                                      --移动游标

   WHILE student_cursor%FOUND                                   --判断游标是否指向某条记录
   LOOP
      OPEN address_cursor;                                          --打开另外一个游标

      FETCH address_cursor INTO add1;                                   --移动游标

      WHILE address_cursor%FOUND                                --判断游标是否指向某条记录
      LOOP
         IF add1.xh = stu1.xh
         THEN                                          --判断两个游标所指向的记录中xh的值是否相等
            UPDATE student s
               SET s.zz = add1.zz
             WHERE CURRENT OF student_cursor;               --假如相等就更新游标所指向的记录值
         END IF;

         FETCH address_cursor INTO add1;                                --移动游标
      END LOOP;

      CLOSE address_cursor;                                             --关闭游标

      FETCH student_cursor INTO stu1;                                   --移动游标
   END LOOP;

   CLOSE student_cursor;                                                --关闭游标
END;

三、REF游标也叫动态游标:

qREF 游标和游标变量用于处理运行时动态执行的 SQL 查询 q创建游标变量需要两个步骤: q声明 REF 游标类型 q声明 REF 游标类型的变量 q用于声明 REF 游标类型的语法为:

TYPE <ref_cursor_name> IS REF CURSOR

[RETURN <return_type>];

-----------------------------------ref游标---------------------------------
declare
type ref_cursor  is ref cursor; --声明一个ref游标类型
tab_cursor ref_cursor ;--声明一个ref游标
sname student.xm %type ;
sno student.xh %type ;
tab_name  );
begin
  tab_name := '&tab_name'; --接收客户输入的表明
  if tab_name = 'student' then
    open tab_cursor for select xh ,xm  from student ; --打开ref游标
    fetch tab_cursor into sno ,sname ;--移动游标
    while tab_cursor %found
      loop
        dbms_output.put_line ('学号:' ||sno ||'姓名:' ||sname );
        fetch tab_cursor into sno ,sname ;
        end loop;
        close tab_cursor ;
        else
          dbms_output.put_line ('没有找到你想要找的表数据信息' );
          end if;
        end;
-----------------------------------ref游标题目---------------------------------
SQL > select * from student ;
        XH KC
---------- ----------
          语文
          数学
          英语
          历史
          语文
          数学
          英语
          语文
          英语
 rows selected

SQL >
完成的任务 :
生成student2表 (xh  ));
对应于每一个学生,求出他的总的选课记录,把每个学生的选课记录插入到student2表中。
即,student2中的结果如下:
                      XH KC
                 --- -------------------------------------------
                        语文数学英语历史
                        语文数学英语
                        语文英语

 ));

declare
kcs  );
kc  );
type ref_cursor is ref cursor; --声明一个ref游标类型
stu_cursor ref_cursor ;--定义一个ref游标类型的变量
type tab_type is table of number; --声明一个table类型
tab_xh tab_type ;--定义一个表类型的变量
cursor cursor_xh is select distinct( xh) from student; --声明一个游标
begin
  open cursor_xh; --打开游标
  fetch cursor_xh bulk collect into tab_xh; --提取数据到表中
   .. tab_xh.count
    loop
      kcs :='' ;
      open stu_cursor for select kc from student s where s.xh = tab_xh(i ); --打开ref游标
      fetch stu_cursor into kc ; --移动游标
      while stu_cursor %found
        loop
          kcs := kc ||kcs ; --连接字符串使用||而不是+
          fetch stu_cursor into kc ; --移动游标
          end loop;
          insert into student2 (xh , kc ) values( i, kcs);
          close stu_cursor ;
      end loop;
      close cursor_xh ;
      end;