您现在的位置是:首页
>
python cursor游标 PL/SQL-嵌套游标cursor
PL/SQL-嵌套游标cur or cur or 函数可以将一个查询结果集封装成一个类似 REF CURSOR 的游标变量 可以 FETCH 记录 也可以作为 REF CURSOR 类型的参数进行传递

PL/SQL-嵌套游标cursor
cursor() 函数可以将一个查询结果集封装成一个类似 REF CURSOR 的游标变量 可以 FETCH 记录 也可以作为 REF CURSOR 类型的参数进行传递 它被称为 嵌套游标(nested cursor) FETCH 记录 我们先看一下测试表 test 和 test 的数据 SQL> select * from test ; A SQL> select * from test ; ID NAME yuechaotian yuechaotian yuechaotian yuechaotian yuechaotian 我们可能会发出这样一个查询 SQL> select id name (select a from test where a = test id) from test ;select id name (select a from test where a = test id) *ERROR 位于第 行:ORA : 单行子查询返回多个行 因为表 test 中有两条 a= 的记录 所以这个查询执行失败了 但有时候我们确实需要这样的查询 怎么办呢?你可以试试 cursor() 函数 SQL> set serveroutput on SQL> declare cursor cur_test is select id name cursor(select a from test where a = test id) from test ; rec_test test %rowtype; cur_test sys_refcursor; rec_test test %rowtype; begin open cur_test ; loop fetch cur_test into rec_test id rec_test name cur_test ; exit when cur_test %notfound; dbms_output put_line( rec_test id: || rec_test id || | rec_test name: || rec_test name); 这里不需要再显式 OPEN 游标 cur_test 也不需要显式关闭 loop fetch cur_test into rec_test ; exit when cur_test %notfound; dbms_output put_line( rec_test a: || rec_test a ); end loop; end loop; close cur_test ; end; /rec_test id: | rec_test name: yuechaotian rec_test a: rec_test a: rec_test id: | rec_test name: yuechaotian rec_test a: rec_test id: | rec_test name: yuechaotian rec_test a: rec_test id: | rec_test name: yuechaotian rec_test id: | rec_test name: yuechaotian PL/SQL 过程已成功完成 怎么样?达到你的目的了吧 我们再看一个嵌套了两个 cursor() 函数的例子 SQL> declare 嵌套定义游标 cursor cur_test is select id name cursor(select a cursor(select * from dual) from test where test a = test id) from test ; cur_test sys_refcursor; cur_dual sys_refcursor; rec_test test %rowtype; rec_test test %rowtype; rec_dual varchar ( ); begin open cur_test ; loop fetch cur_test into rec_test id rec_test name cur_test ; exit when cur_test %notfound; dbms_output put_line( rec_test id: || rec_test id || rec_test name: || rec_test name); 这里不需要再显式 OPEN 游标 cur_test 也不需要显式关闭 loop fetch cur_test into rec_test a cur_dual; exit when cur_test %notfound; dbms_output put_line( rec_test a: || rec_test a ); 这里不需要再显式 OPEN 游标 cur_dual 也不需要显式关闭 loop fetch cur_dual into rec_dual; exit when cur_dual%notfound; dbms_output put_line( rec_dual: || rec_dual ); end loop; end loop; end loop; close cur_test ; end; /rec_test id: rec_test name: yuechaotian rec_test a: rec_dual: Xrec_test a: rec_dual: Xrec_test id: rec_test name: yuechaotian rec_test a: rec_dual: Xrec_test id: rec_test name: yuechaotian rec_test a: rec_dual: Xrec_test id: rec_test name: yuechaotian rec_test id: rec_test name: yuechaotian PL/SQL 过程已成功完成 由以上例子可以看出 嵌套游标是隐式打开的 它在以下情况下被关闭 显式关闭 父游标再次执行时(比如 下一次循环前 会先关闭嵌套游标 再根据新数据重新打开) 父游标关闭时 父游标退出时 fetch 父游标出错时 传递参数 我们先看看测试表中的数据 SQL> select * from test ; ID NAME yuechaotian yuechaotian yuechaotian yuechaotian yuechaotian yuechaotian yuechaotian yuechaotian yuechaotian yuechaotian 已选择 行 SQL> select * from test order by a; A 已选择 行 下面我要查询 test 中的数据 查询条件是 test id 在 test a 中对应的记录数 比如我要查询表 test id 在 test a 中不存在的记录 查询表 test id 在test a 中存在 条的记录 存在 条的记录 存在 条的记录…… 我可以使用嵌套游标实现 SQL> create function f_count(cur_names in sys_refcursor) return number is v_name test name%type; n_count number( ) := ; begin loop fetch cur_names into v_name; exit when cur_names%notfound; n_count := n_count + ; end loop; return n_count; end f_count; / 函数已创建 SQL> select id name from test where f_count( cursor( select a from test where a = test id ) ) = ; ID NAME yuechaotian yuechaotian yuechaotian SQL> select id name from test where f_count( cursor( select a from test where a = test id ) ) = ; ID NAME yuechaotian yuechaotian yuechaotian yuechaotian SQL> select id name from test where f_count( cursor( select a from test where a = test id ) ) = ; ID NAME yuechaotian yuechaotian SQL> select id name from test where f_count( cursor( select a from test where a = test id ) ) = ; ID NAME yuechaotian lishixinzhi/Article/program/Oracle/201311/17957 很赞哦! (1051)