首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Oracle频道
中国IT教育
Google
首页 入门基础 安装配置 体系架构 PLSQL 备份恢复 性能调优 开发技术 资讯动态 考试认证 下载 专题 讨论
您现在的位置: 中国IT实验室 >> Oracle >> PLSQL >> 正文

PL/SQL - 嵌套游标 cursor

 
  怎么样?达到你的目的了吧。我们再看一个嵌套了两个 cursor() 函数的例子:
 
SQL> declare
  2    -- 嵌套定义游标
  3    cursor cur_test2 is
  4      select id, name, cursor(select a, cursor(select * from dual)
  5                                          from test1
  6                                        where test1.a = test2.id)
  7        from test2;
  8    cur_test1 sys_refcursor;
  9    cur_dual  sys_refcursor;
 10    rec_test1 test1%rowtype;
 11    rec_test2 test2%rowtype;
 12    rec_dual varchar2(10);
 13  begin
 14    open cur_test2;
 15    loop
 16      fetch cur_test2 into rec_test2.id, rec_test2.name, cur_test1;
 17      exit when cur_test2%notfound;
 18      dbms_output.put_line( 'rec_test2.id: ' || rec_test2.id || '   rec_test2.name: ' || rec_test2.name);
 19      -- 这里不需要再显式 OPEN 游标 cur_test1,也不需要显式关闭
 20      loop
 21        fetch cur_test1 into rec_test1.a, cur_dual;
 22        exit when cur_test1%notfound;
 23        dbms_output.put_line( 'rec_test1.a: ' || rec_test1.a );
 24        -- 这里不需要再显式 OPEN 游标 cur_dual,也不需要显式关闭
 25        loop
 26          fetch cur_dual into rec_dual;
 27          exit when cur_dual%notfound;
 28          dbms_output.put_line( 'rec_dual: ' || rec_dual );
 29        end loop;
 30      end loop;
 31    end loop;
 32    close cur_test2;
 33  end;
 34  /
rec_test2.id: 1   rec_test2.name: yuechaotian1
rec_test1.a: 1
rec_dual: X
rec_test1.a: 1
rec_dual: X
rec_test2.id: 2   rec_test2.name: yuechaotian2
rec_test1.a: 2
rec_dual: X
rec_test2.id: 3   rec_test2.name: yuechaotian3
rec_test1.a: 3
rec_dual: X
rec_test2.id: 4   rec_test2.name: yuechaotian4
rec_test2.id: 5   rec_test2.name: yuechaotian5
 
PL/SQL 过程已成功完成。
 
        由以上例子可以看出,嵌套游标是隐式打开的。它在以下情况下被关闭:
        显式关闭
          父游标再次执行时(比如,下一次循环前,会先关闭嵌套游标,再根据新数据重新打开)
          父游标关闭时
          父游标退出时
        fetch 父游标出错时
        2. 传递参数
        我们先看看测试表中的数据:
 
SQL> select * from test2;
 
        ID NAME
---------- ---------------------------
         1 yuechaotian1
         2 yuechaotian2
         3 yuechaotian3
         4 yuechaotian4
         5 yuechaotian5
         6 yuechaotian6
         7 yuechaotian7
         8 yuechaotian8
         9 yuechaotian9
        10 yuechaotian10
 
已选择10行。
 
SQL> select * from test1 order by a;
 
         A
----------
         1
         1
         1
         2
         2
         3
         3
         4
         5
         6
 
已选择10行。
 

上一页  [1] [2] [3] 下一页

【责编:michael】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 ·Java入门 Tomcat的配置技巧精华专题…
 ·Oracle RMAN物理备份技术详解…
 今日更新
 社区讨论
 博客论点
 频道精选
 Oracle频道相关导航