`

Oracle Cursor 游标

阅读更多
Oracle中Cursor介绍:
http://www.iteye.com/topic/649874
游标小记(收集汇总):
http://www.itpub.net/viewthread.php?tid=897079&highlight=%D3%CE%B1%EA






REF CURSOR:
Oracle REF CURSOR:
http://www.oradev.com/ref_cursor.jsp
引用
With the REF_CURSOR you can return a recordset/cursor from a stored procedure.

Using Ref Cursors To Return Recordsets:
http://www.oracle-base.com/articles/misc/UsingRefCursorsToReturnRecordsets.php
引用
Since Oracle 7.3 REF CURSORS have been available which allow recordsets to be returned from stored procedures, functions and packages.

REF CURSOR 小结:
http://www.itpub.net/viewthread.php?tid=443352&highlight=ref%2Bcursor
IS CURSOR DIFFERENT FROM REF CURSOR?
http://forums.oracle.com/forums/thread.jspa?threadID=1086826
REF CURSOR需要显式(explicitly)关闭吗?
http://forums.oracle.com/forums/thread.jspa?threadID=627238
引用
Ref cursors otoh are special - as they have no local scope. Only session scope. So as long as that Oracle session exist, that ref cursor created in that session will exist, until explicitly closed. Why? Because ref cursors are intended to be use by the client of that session - which means that irrespective of what happens scope wise on the PL/SQL engine side, that ref cursor handle must "survive" as it can (and often is) used by the client.
This is also the primary cause for cursor leakage. Clients (especially Java apps in my experience) use ref cursors - but forget to close them after use. The open cursor handle count quickly runs up and an ORA error results.. with the Java developers then thinking there is something wrong with Oracle and that the max number of cursor handles per session should be increased.

The oracle's garbage collector does not clean up ref cursor handles - those are only closed either explicitly (you need to code the close) or when the session terminates.
Also, ref cursors are intended for clients to use - not for PL/SQL. So whenever you use a ref cursor in PL/SQL only, you need to ask why. The java/Delphi/C client's ref cursor equivalent in PL/SQL is a DBMS_SQL cursor - that supports a proper call interface, including a describe interface.
Ask The Oracle Expert: Questions & Answers->Closing ref cursor after it's used in a Java program:
http://searchoracle.techtarget.com/answer/Closing-ref-cursor-after-it-s-used-in-a-Java-program
引用
Q:I am sending a variable of ref cursor type to a Java program. Now, how do I close the ref cursor? Does the conn.close in Java close the ref cursors? Is there any way I can explicitly close the same after it is used in Java?
A:With the ref cursor, you're doing everything you need to. That is the way it is done. The caller (your Java program) is responsible for the "close" call -- you don't do that in PL/SQL when the client fetches from the cursor. There's nothing for you to explicitly do any different than you mention.

使用oracle REF CURSOR的例子(scott用户):
CREATE OR REPLACE PROCEDURE p_refcursor_test (
       in_field IN VARCHAR2,
       in_value IN VARCHAR2,
       rslt_cur OUT SYS_REFCURSOR
) IS
  str_sql VARCHAR2(300);
  TYPE t_EmpRecord IS RECORD(
                ename emp.ename%TYPE,
                job emp.job%TYPE,
                sal emp.sal%TYPE
                );
  
  v_EmpRecord t_EmpRecord;
BEGIN
  str_sql := 'SELECT ename,job,sal FROM emp ';
  
  IF(length(in_field)>0) and (length(in_value)>0)
   THEN
       str_sql := str_sql || 'WHERE ' || in_field || ' like ''%' || in_value || '%''';
  END IF;
  
  dbms_output.put_line(str_sql);
  --execute immediate str_sql; --动态执行
  OPEN rslt_cur FOR str_sql;


--如果这个procedure是被java程序调用的(如ibatis中的<procedure>),则这里不可以fetch该rslt_cur,要将下面这个loop循环给注掉!!!否则返回给java ResultSet的结果集将会是空的 
  LOOP
       FETCH rslt_cur INTO v_EmpRecord;
       EXIT WHEN rslt_cur%NOTFOUND;
       DBMS_OUTPUT.PUT_LINE('名字: ' || v_EmpRecord.ename ||
                                        ' 工作: ' || v_EmpRecord.job ||
                                        ' 薪水: ' || v_EmpRecord.sal
                                        );
     
     END LOOP;

--With the ref cursor, you're doing everything you need to. That is the way it is done. The caller (your Java program) is responsible for the "close" call -- you don't do that in PL/SQL when the client fetches from the cursor

  EXCEPTION
    when others then
    DBMS_OUTPUT.PUT_LINE('------>' ||SQLERRM);
END p_refcursor_test;

关于上面代码中的SYS_REFCURSOR:
引用
sys_refcursor是oracle9i以后系统定义的一个refcursor,主要用在过程中返回结果集。 9i之前想要使用ref cursor是要自己定义REF CURSOR类型游标变量的







查看数据库中打开的游标数:http://www.iteye.com/topic/337796
引用
查看游标使用情况:
select o.sid, osuser, machine, count(*) num_curs
from v$open_cursor o, v$session s
where user_name = 'user ' and o.sid=s.sid
group by o.sid, osuser, machine
order by  num_curs desc;

查看游标执行的sql情况:
select q.sql_text
from v$open_cursor o, v$sql q
where q.hashvalue=o.hash_value and o.sid = 123;






将 %ROWTYPE 与游标配合使用:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp?topic=/com.ibm.db2.luw.apdv.plsql.doc/doc/c0053880.html


Oracle/PLSQL: Procedure that outputs a dynamic PLSQL cursor:
http://www.techonthenet.com/oracle/questions/cursor1.php
分享到:
评论

相关推荐

    ORACLE技术文档\oracle cursor 游标.doc

    Oracle游标,或称为光标,是数据库管理系统中用于处理SQL查询的一种机制,它允许程序逐行处理查询结果。在Oracle中,游标是至关重要的,尤其在进行复杂的交互式数据操作时。当一个应用程序尝试打开过多的游标时,...

    快速掌握Oracle数据库游标的使用方法

    Oracle 数据库游标的使用方法 Oracle 数据库游标是指在查询返回结果超过一行时,需要使用的显式游标。用户不能使用 select into 语句,而是需要使用游标来处理多行查询结果。在 PL/SQL 中,隐式游标会在查询开始时...

    oracle 隐式游标,显示游标,游标循环

    ### Oracle中的游标详解 #### 一、引言 在Oracle数据库中,游标是一种非常重要的机制,它允许用户在程序中对查询结果进行逐行处理。游标分为两种主要类型:**隐式游标**和**显示游标**。此外,还可以利用游标进行...

    oracle数据库游标使用例子

    游标(Cursor)是一种机制,它允许我们遍历和操作由SQL查询返回的结果集。通过游标,我们可以控制数据的读取顺序,一次只处理一行,或者在处理完一行后移动到下一行。 2. **游标的属性**: - `%FOUND`:用于检查...

    oracle游标的总结oracle游标的总结

    Oracle 游标概述 Oracle 游标是 Oracle 数据库中的一种重要概念,用于查询数据库,获取记录集合(结果集)的指针。游标可以看作是一个临时表,你可以对其每一行的数据进行任意的操作。本文将对 Oracle 游标的概念、...

    oracle笔记游标的使用

    oracle笔记游标的使用,游标的详细代码案例,游标知识点笔记!

    Oracle的游标学习

    Oracle的游标是数据库开发中的一个重要概念,尤其在PL/SQL编程中,游标用于处理SQL查询结果集,使得我们能够逐行处理数据,而不仅仅局限于一次性获取所有数据。在这个主题中,我们将深入探讨游标的定义、类型、使用...

    ORACLE 游标使用示例

    在Oracle数据库中,游标(Cursor)是一种非常重要的编程工具,尤其在处理复杂的数据操作时。游标允许我们逐行处理查询结果,这对于迭代、条件判断或者数据更新等任务非常实用。下面,我们将深入探讨Oracle游标的使用...

    oracle-游标使用汇总.doc

    Oracle数据库中的游标是处理查询结果集的一种重要机制,尤其在需要逐行处理多行数据时。游标允许我们按照需要遍历查询结果,并对每一行进行操作。本篇文章将详细解析Oracle数据库中游标的使用方法。 首先,游标分为...

    Oracle数据库游标使用大全

    Oracle数据库游标是PL/SQL编程中不可或缺的一部分,它们允许程序逐行处理查询结果,提供了更灵活的数据操作方式。在Oracle数据库中,游标主要分为两种类型:隐式游标和显式游标。 1. **隐式游标**: - 当在PL/SQL...

    非常详细的Oracle游标整理

    Oracle游标是数据库编程中非常重要的一个概念,主要用于处理SQL查询的结果集。游标允许我们按需逐行处理数据,而不是一次性加载所有结果。这里详细介绍了Oracle中的三种游标类型:隐式游标、显式游标和REF游标。 1....

    Oracle入门--Oracle游标使用

    Oracle游标是PL/SQL编程中的重要组成部分,主要用于处理单行或多行查询结果。游标允许程序员逐行处理查询结果,而不是一次性加载所有数据。在Oracle中,游标分为显式游标和隐式游标。 1. **隐式游标**:在PL/SQL中...

    Oracle 游标 Oracle 游标

    在Oracle数据库中,游标(Cursor)是处理数据集的一个关键机制,主要用于执行查询并逐行处理结果集。游标可以被视为一个临时存储区,用于保存SELECT语句的结果集,允许程序对这些结果进行迭代访问。 #### 游标的...

    Oracle显式游标和隐式游标.doc

    Oracle 显式游标和隐式游标 Oracle 中的游标是 SQL 的一个内存工作区,由系统或用户以变量的形式定义。游标有两种类型:显式游标和隐式游标。显式游标是一种明确声明的游标,需要在声明部分使用 CURSOR 语句来定义...

    oracle的游标使用

    在Oracle数据库中,游标(Cursor)是一种用于检索并处理多行数据的强大工具。它允许用户通过循环逐条处理查询结果集中的每一行数据,这对于需要进行复杂数据处理的应用程序来说非常有用。 #### 二、游标分类 游标...

    Oracle游标使用大全

    ### Oracle游标使用详解 #### 一、Oracle游标简介 在Oracle数据库中,游标是一种重要的机制,用于处理查询结果集。它允许用户通过PL/SQL编程语言逐行访问和处理查询返回的数据记录。游标可以是显式定义的(即在...

    oracle游标使用及实例

    在Oracle数据库中,游标(Cursor)是一种用于处理SQL查询结果集的方式。它允许用户逐行地读取和处理查询结果,这对于需要对每一行数据进行特定操作的情况非常有用。游标可以分为显式游标和隐式游标两种类型。 #### 二...

    Oracle游标使用案例大全

    - 游标(Cursor):它是一个数据库系统中的结构,用于存储执行SQL查询后的结果集信息。游标允许我们遍历结果集,并对每一行进行操作。 - 局部游标和全局游标:局部游标在PL/SQL块中定义,仅在该块的范围内有效;...

    Oracle 游标使用大全

    Oracle游标是数据库管理系统中的一种数据处理机制,它允许用户按需逐行处理查询结果,而不是一次性加载所有数据。在Java编程中,我们通常通过JDBC(Java Database Connectivity)来与Oracle数据库交互,其中游标扮演...

Global site tag (gtag.js) - Google Analytics