游标 游标的简介:

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

游标的类型:

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

1
2
3
4
5
6
7
8
begin
  update student s set s.sage = s.sage + 10 ;
  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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
SQL> declare
  2  sname1 student.sname%TYPE;
  3  begin
  4    select sname into sname1 from student;
  5    if sql%found then
  6      dbms_output.put_line(sql%rowcount);
  7    else
  8      dbms_output.put_line('没有找到数据');
  9      end if;
 10      exception
 11        when too_many_rows then
 12          dbms_output.put_line('查找的行记录多于1行');
 13         when no_data_found then
 14            dbms_output.put_line('未找到匹配的行');
 15       end;
 16  /
查找的行记录多于1
PL/SQL procedure successfully completed
 
SQL>

显式游标:



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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
------------------------------------无参数游标-------------------------------
declare
  sname varchar2( 20); --声明变量
  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;
------------------------------------循环游标-------------------------------   
-- Created on 18-1月-15 by 永文
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 where s.sno in (2 ,3 ) for update;--创建更新游标
  begin
   open student_cursor;
   fetch student_cursor into stu1;--移动游标
   while student_cursor%found --遍历游标,判断是否指向某个值
     loop
       update student set sage = sage + 10 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 where s.sno in (2 ,3 ) 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 set sage = sage + 10 where current of student_cursor;--通过游标中的信息更新数据
       end loop;
       close student_cursor;
       end;
------------------------------------使用fetch ... bulk collect into-------------------------------  
declare
  cursor   my_cursor is select ename from emp where deptno= 10; --声明游标
  type   ename_table_type is table of varchar2 (10 );--定义一种表类型,表中的属性列为varchar2类型
  ename_table  ename_table_type;--通过上面定义的类型来定义变量
begin
  open   my_cursor; --打开游标
  fetch my_cursor bulk collect into   ename_table; --移动游标
  for   i in 1 ..ename_table.count  loop
     dbms_output.put_line(ename_table(i));
  end   loop ;
  close my_cursor;
end;
 
 
-----------------------------------显示游标题目--------------------------------------
 
SQL > select * from student ;
        XH XM
---------- ----------
         1 A
         2 B
         3 C
         4 D
 
SQL > select * from address ;
        XH ZZ
---------- ----------
         2 郑州
         1 开封
         3 洛阳
         4 新乡
         
完成的任务 :给表student添加一列zz ,是varchar2 (10 )类型;
再从address中,将zz字段的数值取出来,对应的插入到
student新增的zz列中。
即:得到的结果:student表中,是:
          XH XM         ZZ
         -- ---------- ------
          1 A          开封
          2 B          郑州
          3 C          洛阳
          4 D          新乡
 
 
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>];

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-----------------------------------ref游标---------------------------------
declare
type ref_cursor  is ref cursor; --声明一个ref游标类型
tab_cursor ref_cursor ;--声明一个ref游标
sname student.xm %type ;
sno student.xh %type ;
tab_name varchar2 (20 );
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
---------- ----------
         1 语文
         1 数学
         1 英语
         1 历史
         2 语文
         2 数学
         2 英语
         3 语文
         3 英语
9 rows selected
 
SQL >      
完成的任务 :
生成student2表 (xh number, kc  varchar2 (50 ));
对应于每一个学生,求出他的总的选课记录,把每个学生的选课记录插入到student2表中。
即,student2中的结果如下:
                      XH KC
                 --- -------------------------------------------
                       1 语文数学英语历史
                       2 语文数学英语
                       3 语文英语
 
create table student2 (xh number, kc varchar2 (50 ));
 
declare
kcs varchar2 (50 );
kc varchar2 (50 );
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; --提取数据到表中
  for i in 1 .. 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;

【转】oracle中的游标的原理和使用详解的更多相关文章

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

    游标的简介 逐行处理查询结果,以编程的方式访问数据. 游标的类型: 1,隐式游标:在 PL/SQL 程序中执行DML SQL 语句时自动创建隐式游标,名字固定叫sql. 2,显式游标:显式游标用于处理 ...

  2. oracle中的exists 和not exists 用法详解

    有两个简单例子,以说明 “exists”和“in”的效率问题 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; ...

  3. oracle中的exists 和not exists 用法详解(转)

    有两个简单例子,以说明 “exists”和“in”的效率问题 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; ...

  4. oracle中去掉回车换行空格的方法详解

    函数: 1.translate语法:TRANSLATE(char, from, to)用法:返回将出现在from中的每个字符替换为to中的相应字符以后的字符串.            若from比to ...

  5. oracle中检索结果汉字首字母排序详解

    今天写需求,要求将结果按照成本中心首字母排序,平且空放在最前面. 进入正题: 1.使用oracle自带的函数: 按照首字母排序:nlssort(xxx,'NLS_SORT=SCHINESE_PINYI ...

  6. oracle 中的游标

    oracle 中的游标 通俗易懂的sql代码直接上! --简单的游标使用滴呀 --使用FOR OBJ IN OBJS LOOP ......END LOOP; DECLARE CURSOR C_JOB ...

  7. Oracle中使用游标转换数据表中指定字段内容格式(拼音转数字)

    应用场景:将数据表TB_USER中字段NNDP的内容中为[sannanyinv]转换为[3男1女] 主要脚本:一个游标脚本+分割字符串函数+拼音转数字脚本 操作步骤如下: 1.创建类型 create ...

  8. Oracle中使用游标获取指定数据表的所有字段名对应的字符串

    操作步骤:打开PLSQL Developer后,直接执行下面的语句就可以出来 --Oracle中使用游标获取指定数据表的所有字段名对应的字符串 declare mytablename VARCHAR( ...

  9. Oracle中的SQL分页查询原理和方法详解

    Oracle中的SQL分页查询原理和方法详解 分析得不错! http://blog.csdn.net/anxpp/article/details/51534006

随机推荐

  1. jQuery插件EasyDrag轻松实现JS拖动的效果

    jquery.easydrag下载地址:http://ishare.iask.sina.com.cn/f/34289681.html 实现效果图:分布实现一.页面Html标签元素定义 <!doc ...

  2. Android Studio找不到FragmentActivity类

    右击项目——>open module settings——>选择第五个选项卡“Dependencies”——>点击加号——>选择第一个Library dependency——& ...

  3. 谁说C语言很简单?

    前两天,Neo写了一篇<语言的歧义>其使用C语言讨论了一些语言的歧义.大家应该也顺便了解了一下C语言中的很多不可思异的东西,可能也是你从未注意到的东西. 是的,C语言并不简单,让我们来看看 ...

  4. 近期对招聘Android开发者的一些思考

    公司要招聘Android开发者,故面试了大概十来个人.由于是小公司,所以来的人大多是90后,比較年轻.90后大概二十三四岁吧,从简历上看都写了一到两年的工作经验. 也由于是小公司,所以对工作经验这些没 ...

  5. 案例:用Redis来存储关注关系

    Redis提供了丰富的数据类型,比起关系型数据库或者简单的Key-Value存储(比如Memcached)来,Redis的数据模型与实际应用的数据模型更相近.比如下面说到的好友关系的存储,原作者使用了 ...

  6. JAXB--@XmlElementWrapper注解和泛型一起使用

    当java对象的某个属性使用泛型时,普通对象都没问题,但是遇到HashSet这种集合类封装的元素时,就会出现元素内容序列化不出来的问题,详见如下: 一.示例: 第一步:定义java对象 package ...

  7. Java:集合,对列表(List)中的自定义对象按属性(字段)排序(正序、倒序)的方法

    1. 要求 对列表(List)中的自定义对象,要求能够按照对象的属性(字段)进行排序(正序.倒序). 如:用户对象(Member)有用户名(username).级别(level).出生日期(birth ...

  8. LVL类及接口使用介绍(License Verification Library )

    原文:http://android.eoe.cn/topic/android_sdk LVL Classes and Interfaces-LVL类和接口 Table 1 lists all of t ...

  9. PCIE协议解析 synopsys IP loopback 读书笔记(1)

    1      Overview Core支持单个Pcie内核的Loopback功能,该功能主要为了做芯片验证,以及在没有远程接收器件的情况下完成自己的回环.同时,Core也支持有远程接收器件的loop ...

  10. 【Unity】6.8 Quaternion类(四元数)

    分类:Unity.C#.VS2015 创建日期:2016-04-20 一.四元数的概念 四元数包含一个标量分量和-个三维向量分量,四元数Q可以记作: Q=[w,(x,y,z)] 在3D数学中使用单位四 ...