Need to explicity close ref cursor?

Happy Friday. I've been trying to test out this one myself, but the results are inconclusive, sooo... Say if you declare a cursor and a ref cursor as rowtype of that cursor in the specification of a package, and in the body if you 'open cursor for', have you then got to explicity close the cursor?
If anyone's got a query against the v$ tables that would prove/disprove the above it would be much appreciated.
Thanks in advance.

Oracle (aka PL/SQL engine) has a garbage collector. It will close cursor handles when that handle goes out of scope.
So you need not close explicit cursor handles for example that is defined in a local scope (though it is good programming practice to do so), whereas you should close it when that cursor handle resides within the scope of a package's state. (i.e. defined in the package header or body as a global)
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.
PS. typing this on my home console (Windows) and not work machine (Linux), so unfortunately no SQL code snippets.. but you can have a look at the Reference Manual and look for the V$ cursor views. There is an open cursor view that lists open cursors per session - just note that garbage collection can happen at the start of the next call and not at the end of the last call.. if I recall correctly. Which at times make the results look "funny" when tracing open cursor handles.

Similar Messages

  • Ref cursors for stored procedure

    Hi every body,
    I am new one,I am in learning stage.
    My java developer was ask me give set of results.
    i am using in my code sys_refcursor, my questions are.
    SQL> create or replace procedure sysref(p_out out sys_refcursor) is
    2 begin
    3 open p_out for select empno,ename from employee;
    4 end;
    5 /
    I am using the procedure like above procedure.
    1) How to use ref cursors in procedure. what is the main use.
    2) How to close ref cursors in procedure.If it is need.
    3) How to use exceptions in procedures for ref cursors.
    Give me one example with complete structure.
    Please help me
    new one
    Edited by: subba on Aug 19, 2010 12:08 AM
    Edited by: subba on Aug 19, 2010 12:09 AM

    subba wrote:
    1) How to use ref cursors in procedure. what is the main use.All SQL in Oracle are parsed as cursors in the Shared Pool. The client code (be that PL/SQL or Visual Basic or Java) gets a handle or reference to this SQL cursor on the server.
    Using this handle, the client can fetch rows using the cursor (the cursor is like a program - the executable binary version of the source SQL code).
    Clients implement their interfaces with this SQL cursor handle in different ways. PL/SQL alone supports:
    - implicit cursor handles
    - explicit cursor handles
    - DBMS_SQL cursor handles
    - reference cursor handles
    Why so many? Different tools for different jobs. Each has its pros and cons and each is suited for specific (and different) types of problems.
    A ref cursor is special in that PL/SQL can pass this SQL cursor reference handle to an external client to use - like Java.
    Why?
    The basic concept is that of abstraction. Removing the complexities of SQL and the database design and SQL optimisation from the Java code. Instead, Java calls a PL/SQL proc. This proc creates the SQL cursor, and uses a reference cursor variable to store the SQL cursor handle. It can then pass this to Java. This means that the entire SQL is encapsulated in the PL/SQL proc. We can now modify it, optimise it, support data models changes in the database via the PL/SQL proc... all without even having to recompile the Java code that makes the call and consumes the ref cursor the PL/SQL proc gives it.
    2) How to close ref cursors in procedure.If it is need.Yes. A very important factor - well spotted.
    As PL/SQL can pass ref cursors to external clients. it does not automatically close these cursors. It does not know when the client has consumed the cursor. It will only close it when the cursor is still open, when the Oracle server session is terminated.
    So it is important that the client (Java in this case) closes the cursor when done. If not, the cursor will remain open, will continue to occupy server resources... and if the client opens more and more of these cursors, the server process will run out of resources.
    Cursor leakage by Java developers are common - and can simply be avoided by closing the cursor once the Java code has consumed it.
    3) How to use exceptions in procedures for ref cursors.No. Why? Why would you want to deal with the exception in a PL/SQL proc? The Java code wants to open that cursor. It knows why. It knows what to do when that cursor fails or is empty. It interacts with the user. How is the PL./SQL proc, whose only function is to construct the SQL and pass the cursor handle to Java, to know any of this?
    Exceptions in this regard should typically be handled in Java. Not in PL/SQL.
    Of course, if that PL/SQL proc has parameters, it also needs to validate these and it needs to raise application exceptions to inform the Java caller when these parameters are invalid.
    But actually catching exception in this case... does not make much sense. As what can the PL/SQL do about the error when it is a "server" and Java is the "client"?
    It is like saying that the Oracle Server should handle all exceptions when TOAD is used and incorrect SQL is passed to it. How does it make sense for Oracle to process SQL errors and the TOAD client not getting any results from a wrong SQL and not knowing that there was even an error with that SQL?

  • Can we open and close the cursor again and again

    hi all,
    with the help of cursor select statement i am writing some columns into the text file. while writing into the file in between i want to write something which belongs to others cursor. shall i close the first cursor and reopen it again..
    is its works?

    If you are using for loop the you don't need to open and close the cursor like:
    SQL> set autot off
    SQL> begin
      2     for i in (select ename, deptno, sal from emp)
      3     loop
      4             for j in (select dname from dept where deptno = i.deptno)
      5             loop
      6             dbms_output.put_line ('Ename:' || i.Ename ||'  Dept Name:'|| j.dname);
      7             end loop;
      8     end loop;
      9  end;
    10  /
    PL/SQL procedure successfully completed.
    SQL> set serveroutput on
    SQL> /
    Ename:SCOTT  Dept Name:RESEARCH
    Ename:first_0  Dept Name:SALES
    Ename:first_1  Dept Name:SALES
    Ename:first_0  Dept Name:RESEARCH
    Ename:first_1  Dept Name:SALES
    Ename:first_0  Dept Name:SALES
    Ename:first_1  Dept Name:ACCOUNTING
    Ename:first_0  Dept Name:RESEARCH
    Ename:first_1  Dept Name:ACCOUNTING
    Ename:first_0  Dept Name:SALES
    Ename:first_1  Dept Name:RESEARCH
    Ename:first_0  Dept Name:SALES
    Ename:first_1  Dept Name:RESEARCH
    Ename:first_0  Dept Name:ACCOUNTING
    PL/SQL procedure successfully completed.
    SQL> Or let us know about your requirements i.e. why you need to open/close the cursor again and again.

  • Dynamic Ref Cursor report in 6i

    Hello,
    I apologise that this question is so similar to many in this forum, but I haven't found a solution to my problem yet.
    I've created a ref cursor query because I need to include a variable p_orgs in my WHERE clause, like so:
    where e.org_id in ('||p_orgs||')
    I've constructed the report as mentioned on this page:
    www.dulcian.com
    FAQs - SQL & PL/SQL FAQs - FAQ ID# 5:
    "How can you use 'dynamic' ref cursors in Oracle Reports 3.0 / 6i?"
    (Thanks to Zlatko Sirotic for the information).
    I get a compile error when compiling the package body that holds my ref cursor open statement:
    Error 103 Encountered the symbol ''select'' when expecting one of the following:
    Select
    Is there any way around this problem?
    If I was to upgrade to Reports 9i would it work?
    Many thanks,
    Hazel

    Hello,
    Just a remark : you don't need to use a Ref Cursor if you just want to use a "dynamic where clause".
    You can use a lexical reference :
    You can find examples at :
    http://www.oracle.com/webapps/online-help/reports/10.1.2/topics/htmlhelp_rwbuild_hs/rwwhthow/whatare/dmobj/sq_a_lexical_references.htm
    (This page is about Reports 10.1.2 but Lexical references are identical in Reports 6i and Reports 10.1.2)
    Regards

  • Manipulating data from a ref cursor in the data block of a form

    I am using Oracle Forms 10g. I have a ref cursor which returns columns A, B, C, D, E where B, C, D, E are values. The "Query Data Source Name" attribute in the datablock contains "CCTR_Extract_pkg.cctr_refcur". The "Column Name" attribute of item A contains "A", item B contains "B", etc. All of these columns are displayed on the form and it all works perfectly.
    I have a new request, the users would like a 6th column displayed - column F - where it equals D - B.
    Is there any way of doing this in the form only or do I need to change the ref cursor to accomodate the new column and then the form?
    If it can be achieved in the Form (only) - then how do I reference the 2 columns?
    Thanks in anticipation
    Michael
    Edited by: user8897365 on 24-Aug-2011 10:32

    Is there any way of doing this in the form only or do I need to change the ref cursor to accomodate the new column and then the form? The REF_CURSOR is the data source of your block so you will have to modify the CCTR_Extract_pkg package and cctr_refcur REF_CURSOR.
    If it can be achieved in the Form (only) - then how do I reference the 2 columns?After you have modified your database package, I recommend you run the Data Block Wizard on your block and let Forms detect the 2 new columns. You can do this manually, but it is safer to let Forms do it for you. If you want to do it manually, open the Query Data Source Columns property and add your new columns.
    Hope this helps,
    Craig B-)
    If someone's response is helpful or correct, please mark it accordingly.

  • How to put a collection into a Ref Cursor?

    Hi,
    I am trying to create a procedure (inside a package) which fills a collection and I need to fill a ref cursor (out parameter) with this collection. I can fill the collection but I how can I fill the ref cursor? I am receiving the message "PL/SQL: ORA-00902: invalid datatype" (Highlighted below as comments)
    I have a limitation: I am not allowed to create any kind of objects at the database schema level, so I have to create them inside the package. I'm writting it with SQL Tools 1.4, I'm also not allowed to do this in SQL+.
    This is the code of the package. The cursors' selects were simplified just because they are not the problem, but their structure is like follows below.
    CREATE OR REPLACE PACKAGE U3.PKG_TESTE AS
    TYPE REC_TYPE IS RECORD(
    COL1 VARCHAR2(50) ,
    COL2 VARCHAR2(100) ,
    COL3 VARCHAR2(20) ,
    COL4 VARCHAR2(30) ,
    COL5 VARCHAR2(100) ,
    COL6 VARCHAR2(50) ,
    COL7 NUMBER(3) ,
    COL8 VARCHAR2(30) ,
    COL9 VARCHAR2(16) ,
    COL10 VARCHAR2(50) ,
    COL11 NUMBER(4) ,
    COL12 VARCHAR2(40)
    TYPE REC_TYPE_LIST IS TABLE OF REC_TYPE
    INDEX BY BINARY_INTEGER;
    TYPE C_RESULTSET IS REF CURSOR;
    VAR_TAB_TESTE     REC_TYPE_LIST;
    PROCEDURE     Z_REC_INSTANCE
    pUSER_SYS_CODE VARCHAR2,
    pSYS_SEG_CODE VARCHAR2,
    pComplFiltro VARCHAR2,
    pCodInter NUMBER,
    cResultset out C_RESULTSET
    END PKG_TESTE ;
    CREATE OR REPLACE PACKAGE BODY U3.PKG_TESTE
    AS
    PROCEDURE Z_REC_INSTANCE
    pUSER_SYS_CODE varchar2,
    pSYS_SEG_CODE varchar2,
    pComplFiltro varchar2,
    pCodInter number
    AS
    cursor cur1 is
    select 'A' COL1, 'B' COL2, 'C' COL3, 'D' COL4, 'E' COL5,
    'F' COL6, 'G' COL7, 'H' COL8
    FROM DUAL;
    regCur1 cur1%rowtype;
    cursor cur2 is
    SELECT 'I' C1, 'J' C2, 'K' C3, 'L' C4
    FROM DUAL;
    regCur2 cur2%rowtype;
    varSQL varchar2(4000);
    varCOL10s varchar2(100);
    varFiltroAtrib varchar2(100);
    varCount number(10);
    BEGIN
    varCount := 1;
    open cur1;
    Loop
    fetch cur1 into regCur1;
    exit when cur1%notfound;
    open cur2;
    Loop
    fetch cur2 into regCur2;
    exit when cur2%notfound;
    VAR_TAB_TESTE(varCount).COL1 := regCur1.COL1;
    VAR_TAB_TESTE(varCount).COL2 := regCur1.COL2;
    VAR_TAB_TESTE(varCount).COL3 := regCur1.COL3;
    VAR_TAB_TESTE(varCount).COL4 := regCur1.COL4;
    VAR_TAB_TESTE(varCount).COL5 := regCur1.COL5;
    VAR_TAB_TESTE(varCount).COL6 := regCur1.COL6;
    VAR_TAB_TESTE(varCount).COL7 := regCur1.COL7;
    VAR_TAB_TESTE(varCount).COL8 := regCur1.COL8;
    VAR_TAB_TESTE(varCount).COL9 := regCur2.C1;
    VAR_TAB_TESTE(varCount).COL10 := regCur2.C2;
    VAR_TAB_TESTE(varCount).COL11 := regCur2.C3;
    VAR_TAB_TESTE(varCount).COL12 := regCur2.C4;
    varCount := varCount + 1;
    end Loop;
    end Loop;
    -- I'd like to do something like this:
    -- c_resultset := select * from var_tab_teste;
    -- but i don't know how to put the records of the type on the ref cursor,
    -- probably because I don't know how to select them
    -- pl/sql: ora-00902: invalid datatype
    for varCount in (select COL1 from table( CAST ( VAR_TAB_TESTE AS REC_TYPE_LIST ) ))
    loop
    dbms_output.put('WORKS');
    end loop;
    END Z_REC_INSTANCE;
    END PKG_TESTE;
    SHOW     ERR     PACKAGE          PKG_TESTE;
    SHOW     ERR     PACKAGE BODY     PKG_TESTE;
    SHOW     ERR     PROCEDURE     PKG_TESTE.Z_REC_INSTANCE;
    I'm using:
    Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
    With the Partitioning, OLAP and Oracle Data Mining options
    JServer Release 9.2.0.1.0 - Production
    Thanks in advance.

    I don't have the exact version but in 9iOK I lied, I found a 9i instance ;-)
    Oracle9i Enterprise Edition Release 9.2.0.7.0 - 64bit Production
    JServer Release 9.2.0.7.0 - Production
    SQL> CREATE TABLE table_name (column_name VARCHAR2 (30));
    Table created.
    SQL> INSERT INTO table_name VALUES ('value one');
    1 row created.
    SQL> INSERT INTO table_name VALUES ('value two');
    1 row created.
    SQL> COMMIT;
    Commit complete.
    SQL> CREATE OR REPLACE PACKAGE package_name
      2  AS
      3     TYPE collection_type_name IS TABLE OF table_name%ROWTYPE;
      4
      5     FUNCTION function_name
      6        RETURN collection_type_name PIPELINED;
      7  END package_name;
      8  /
    Package created.
    SQL> CREATE OR REPLACE PACKAGE BODY package_name
      2  AS
      3     FUNCTION function_name
      4        RETURN collection_type_name PIPELINED
      5     IS
      6     BEGIN
      7        FOR record_name IN (SELECT column_name
      8                            FROM   table_name) LOOP
      9           PIPE ROW (record_name);
    10        END LOOP;
    11
    12        RETURN;
    13     END function_name;
    14  END package_name;
    15  /
    Package body created.
    SQL> VARIABLE variable_name REFCURSOR;
    SQL> BEGIN
      2     OPEN :variable_name FOR
      3        SELECT column_name
      4        FROM   TABLE (package_name.function_name);
      5  END;
      6  /
    PL/SQL procedure successfully completed.
    SQL> PRINT variable_name;
    COLUMN_NAME
    value one
    value two
    SQL>I recommend though that you test this thoroughly. There were bugs with this approach when it was newly introduced that prevented you from dropping the package.

  • Ref Cursor Using Dynamic Query

    Hi,
    I need to use the ref cursor to fetch result from dynamic query.
    e.g.
    Open ref_test_tbl for Select * from tbl1 where tbl1.field1= :1
    and tbl1.field2 =:2 using i_v1,i_v2;
    The thing is i_v1, i_v2 are dynamic.
    i.e. using clause can include i_v3, i_v4 also.
    How to include dynamic variables in the using clause.
    thanks

    > How to include dynamic variables in the using clause.
    Cannot using a ref cursor.. (and anyone that post code that writes dynamic PL/SQL in order to achieve this, I will call an idiot).
    What you should be using in PL/SQL is a DBMS_SQL cursor. This allows you to create a fully dynamic SQL statement with bind variables. E.g.
    select * from tbl1 where col1 = :1
    select * from tbl1 where col2 = :1 order by 2
    select * from tbl1 where col3 between :0 and :1
    Using this dynamically created SQL statement, you can parse it using DBMS_SQL, determine the number of bind variables, and bind each of these dynamically.
    You can then execute the SQL and again, dynamically, determine just what the projection of the cursor is. How many columns are returned, their names, data types, precision and so on.
    This is what APEX (see http://apex.oracle.com) use extensively in order to run SQLs and render these as reports - all dynamically.
    DBMS_SQL is detailed in the [url http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_sql.htm#BABEDAHF]Oracle® Database PL/SQL Packages and Types Reference manual.

  • REF CURSOR problem .. function call from select

    Hello,
    I am still on the search for a way to call a function from with in a select statement.
    I've done it in SQL*Plus like this and it works.
    SELECT name, f_format(name) FROM user WHERE id = 12;
    This returns first the unformatted (raw) version of the stored value for name and then the formmtted version of the stored value of name.
    Since multiple names will be returned I need to user a REF CURSOR (cursor variable). The above select statement does not work from within the OPEN . .FOR clause necessary for a REF CURSOR.
    What can I do instead? My goal is to return multiple records of formatted names.
    null

    I have declared the reference cursor as a weak one. Thank you for clarifying that though. I was wondering if it made a difference. Here is a simple package that shows you what I am trying to do. I will include the errors at the bottom. Please have a look and let me know what you think is wrong.
    /*--------- PL/SQL ------------*/
    CREATE OR REPLACE PACKAGE test_refcur AS
    PROCEDURE decrypt (
    i_string IN VARCHAR2
    ,o_string OUT VARCHAR2
    TYPE PwdCurTyp IS REF CURSOR;
    PROCEDURE get_pwd_info (
    i_user_id IN NUMBER
    ,io_pwd_cv IN OUT PwdCurTyp
    END test_refcur;
    CREATE OR REPLACE PACKAGE BODY test_refcur AS
    FUNCTION f_decrypt (
    i_string IN VARCHAR2
    ) RETURN VARCHAR2 IS
    l_string VARCHAR2(64);
    l_string_dec VARCHAR2(32);
    BEGIN
    l_string := i_string;
    dbms_obfuscation_toolkit.DESDecrypt(
    input_string => l_string
    ,key_string =>'12345678'
    ,decrypted_string=> l_string_dec );
    RETURN l_string_dec;
    END f_decrypt;
    PROCEDURE decrypt (
    i_string IN VARCHAR2
    ,o_string OUT VARCHAR2
    ) IS
    BEGIN
    o_string := f_decrypt(i_string);
    END decrypt;
    PROCEDURE get_pwd_info (
    i_user_id IN NUMBER
    ,io_pwd_cv IN OUT PwdCurTyp
    ) IS
    BEGIN
    OPEN io_pwd_cv FOR
    SELECT
    label
    ,f_decrypt(user_name_text) AS dec_user_name
    ,f_decrypt(text) AS dec_pwd
    FROM
    code_pwd
    WHERE
    id = i_user_id;
    END get_pwd_info;
    END test_refcur;
    /*--------- SQL*Plus -----------*/
    Warning: Package Body created with compilation errors.
    SHOW ERRORSErrors for PACKAGE BODY TEST_REFCUR:
    LINE/COL ERROR
    28/13 PL/SQL: SQL Statement ignored
    30/18 PLS-00231: function 'F_DECRYPT' may not be used in SQL
    Then I made the SELECT statement dynamic.
    OPEN io_pwd_cv FOR
    'SELECT label, f_decrypt(user_name_text) AS dec_user_name, f_decrypt(text) AS dec_pwd FROM code_pwd WHERE id = i_user_id';
    It compiled fine.
    Package body created.
    So then I tried to do the following. .
    SET AUTOPRINT ON
    SET SERVEROUTPUT ON
    VARIABLE cv REFCURSOR
    EXECUTE test_refcur.get_pwd_info(18, :cv)begin test_refcur.get_pwd_info(18, :cv); end;
    ERROR at line 1:
    ORA-00904: invalid column name
    ORA-06512: at "K.TEST_REFCUR", line 27
    ORA-06512: at line 1
    If I change the SELECT statement to the following, this is what I get. .
    OPEN io_pwd_cv FOR
    SELECT label
    -- ,f_decrypt(user_name_text) AS dec_user_name
    -- ,f_decrypt(text) AS dec_pwd
    FROM code_pwd
    WHERE id = i_user_id;
    SET AUTOPRINT ON
    SET SERVEROUTPUT ON
    VARIABLE cv REFCURSOR
    EXECUTE test_refcur.get_pwd_info(18, :cv)PL/SQL procedure successfully completed.
    LABEL
    Development Server
    That tells me that at least something is working. How can I get everything else to work?

  • How to return integer used wit DMBS_SQL as ref cursor ?

    I am trying to return a ref cursor form a procedure to be used by xsql to output hierarchical XML.
    I created the query using the DBMS_SQL package, did an OPEN_CURSOR and then PARSE.
    Can I, and if so how, return the integer from the PARSE as a ref cursor out of my procedure ? According to the docs the integer in the PARSE is the ID number for a cursor.
    thanks,
    Reinier

    I suspect that you are a little confused. I doubt that what you are asking for is what you really need. I believe you want the contents of a ref cursor, not an integer that is the cursor id. Since you are using Oracle 8i, as mentioned in your other post, you don't need DBMS_SQL; You just need to open a ref cursor dynmacially. Please see my response to your other post:
    Re: Error while compiling form in AS 10g  deployed on Linux

  • Cursor difference (REF Cursor & Sys_Refcursor)

    Hi,
    Is there any difference between REF Cursor and SYS_Refcursor?
    I see some packages using sys_refcursor and some using REF Cursor.
    Package 1 (Spec):
       PROCEDURE s_demo1 (
          ret_rset   OUT      sys_refcursor
    Package 2 (Spec):
    TYPE cur_set IS REF CURSOR;
    PROCEDURE s_test1 (out_data OUT cur_set);
    PROCEDURE s_test2 (out_data OUT cur_set);I am going through this link (http://sql-plsql.blogspot.com/2007/05/oracle-plsql-ref-cursors.html) but didn't find the answer.

    DomBrooks wrote:
    John - don't get me wrong, I'm not suggesting that as soon as something like SYS_REFCURSOR comes along, everything should get rewritten. It's just I still see new packages written on 11g by, say mainly C# or java developers, which use this old style.
    But.... are you saying that you can't do this?
    CREATE OR REPLACE PACKAGE old_package AS
    TYPE pkg_cur IS REF CURSOR;
    FUNCTION foo(p_id IN NUMBER) RETURN sys_refcursor;
    END;
    CREATE OR REPLACE PACKAGE BODY old_package AS
    FUNCTION foo(p_id IN NUMBER) RETURN sys_refcursor IS
    l_cur sys_refcursor;
    BEGIN
    OPEN l_CUR FOR SELECT * FROM dual WHERE 1 = p_id;
    RETURN l_cur;
    END;
    END;
    Absolutely, I can even declare pkg_cursor as a sys_refcursor and all of the old calls will still work since sys_refcursor is just a ref cursor in disguise. What I cannot do is drop the declaration in the package spec since the outside callers rely on it, so since sys_refcusor is just a ref cursor in disguise, why use it?
    In fact, in OHOME/rdbms/admin/stdspec.sql which is the declarations for the package standard sys_refcursor is declared as:
      /* Adding a generic weak ref cursor type */
      type sys_refcursor is ref cursor;Since things declared in standard package (like data types, operators, all of the named exceptions etc.) are "globally" available without using the package name, sys_refcursor just provides a method to save a few keystrokes everywhere. It is no different functionally than a package declared ref cursor. The main difference is that you cannot create a strongly typed sys_refcursor, if you want/need one, you need to use a ref cursor.
    I do tend to use sys_refcursor in new code, but it took me a while to switch because there is not much real benefit as far as I can see.
    John
    Edited by: John Spencer on Nov 17, 2010 4:11 PM

  • DB proc - do you need to create a table to pass a ref cursor record type?

    I want to pass a limited selection of columns from a large table through a DB procedure using a REF CURSOR, returning a table rowtype:
    CREATE OR REPLACE package XXVDF_XPOS_DS021_ITEMS AS
         TYPE XXVDF_XPOS_DS021_ITEM_ARRAY
         IS REF CURSOR
         return XXVDF_XPOS_DS021_ITEM_TABLE%ROWTYPE;
    Do I need to create this dummy table?
    I can't get a TYPE to work, where the type is an OBJECT with the desired columns in it.
    So a dummy empty table will sit in the database...
    Is there another way?
    thanks!

    You can use RECORD type declaration:
    SQL> declare
      2   type rec_type is record (
      3    ename emp.ename%type,
      4    sal emp.sal%type
      5   );
      6   type rc is ref cursor return rec_type;
      7   rc1 rc;
      8   rec1 rec_type;
      9  begin
    10   open rc1 for select ename, sal from emp;
    11   loop
    12    fetch rc1 into rec1;
    13    exit when rc1%notfound;
    14    dbms_output.put_line(rec1.ename || ' ' || rec1.sal);
    15   end loop;
    16   close rc1;
    17  end;
    18  /
    SMITH 800
    ALLEN 1600
    WARD 1250
    JONES 2975
    MARTIN 1250
    BLAKE 2850
    CLARK 2450
    SCOTT 3000
    KING 5000
    TURNER 1500
    ADAMS 1100
    JAMES 950
    FORD 3000
    MILLER 1300or use, for example, VIEW to declare rowtype:
    SQL> create view dummy_view as select ename, sal from emp;
    View created.
    SQL> declare
      2   type rc is ref cursor return dummy_view%rowtype;
      3   rc1 rc;
      4   rec1 dummy_view%rowtype;
      5  begin
      6   open rc1 for select ename, sal from emp;
      7   loop
      8    fetch rc1 into rec1;
      9    exit when rc1%notfound;
    10    dbms_output.put_line(rec1.ename || ' ' || rec1.sal);
    11   end loop;
    12   close rc1;
    13  end;
    14  /
    SMITH 800
    ALLEN 1600
    WARD 1250
    JONES 2975
    MARTIN 1250
    BLAKE 2850
    CLARK 2450
    SCOTT 3000
    KING 5000
    TURNER 1500
    ADAMS 1100
    JAMES 950
    FORD 3000
    MILLER 1300 Rgds.

  • Do I need close a Cursor Variable after FETCH the value?

    I have this following code and I don't have OPEN or For LOOP before fetch
    All I need is to get the value from ref cursor (if it exist and it should only have one value in it) ; if there is no record, then send to error.
    What I'm not sure is if I need the closing statement at the end of FETCH.
    -- x_cursor is a ref cursor
    oracle_form.lov(p_lov_rec => v_lov_rec, x_cursor => x_cursor);
    FETCH x_cursor INTO MyValue_hold;
    IF x_cursor%NOTFOUND THEN
    x_return_status := 'INVALID';
    ELSE
    x_return_status := 'VALID';
    END IF;
    CLOSE x_cursor; --- Do I need this close statement ??
    Thank you

    It is good practice to always explicitly close cursors that have been explicitly opened.
    Whilst oracle does implicitly close cursors, this is not necessarily done immediately i.e. there may be a delay in oracle sorting out the garbage. Leaving cursors open can result in the "TOO MANY OPEN CURSORS" error if you're not careful.

  • Ref Cursor over Implicit and explicit cursors

    Hi,
    In my company when writing PL/SQL procedure, everyone uses "Ref Cursor",
    But the article below, says Implicit is best , then Explicit and finally Ref Cursor..
    [http://www.oracle-base.com/forums/viewtopic.php?f=2&t=10720]
    I am bit confused by this, can any one help me to understand this?
    Thanks

    SeshuGiri wrote:
    In my company when writing PL/SQL procedure, everyone uses "Ref Cursor",
    But the article below, says Implicit is best , then Explicit and finally Ref Cursor..
    [http://www.oracle-base.com/forums/viewtopic.php?f=2&t=10720]
    I am bit confused by this, can any one help me to understand this?There is performance and there is performance...
    To explain. There is only a single type of cursor in Oracle - that is the cursor that is parsed and compiled by the SQL engine and stored in the database's shared pool. The "+client+" is then given a handle (called a SQL Statement Handle in many APIs) that it can use to reference that cursor in the SQL engine.
    The performance of this cursor is not determined by the client. It is determined by the execution plan and how much executing that cursor cost ito server resources.
    The client can be Java, Visual Basic, .Net - or a PL/SQL program. This client language (a client of SQL), has its own structures in dealing with that cursor handle received from the SQL engine.
    It can hide it from the developer all together - so that he/she does not even see that there is a statement handle. This is what implicit cursors are in PL/SQL.
    It can allow the developer to manually define the cursor structure - this is what explicit cursors, ref cursors, and DBMS_SQL cursors are in PL/SQL.
    Each of these client cursor structures provides the programmer with a different set of features to deal with SQL cursor. Explicit cursor constructs in PL/SQL do not allow for the use of dynamic SQL. Ref cursors and DBMS_SQL cursors do. Ref cursors do not allow the programmer to determine, at run-time, the structure of the SQL projection of the cursor. DBMS_SQL cursors do.
    Only ref cursors can be created in PL/SQL and then be handed over to another client (e.g. Java/VB) for processing. Etc.
    So each of the client structures/interfaces provides you with a different feature set for SQL cursors.
    Choosing implicit cursors for example does not make the SQL cursor go faster. The SQL engine does not know and does not care, what client construct you are using to deal with the SQL cursor handle it gave you. It does not matter. It does not impact its SQL cursor performance.
    But on the client side, it can matter - as your code in dealing with that SQL cursor determines how fast your interaction with that SQL cursor is. How many context switches you make. How effectively you use and re-use the SQL (e.g. hard parsing vs soft parsing vs re-using the same cursor handle). Etc.
    Is there any single client cursor construct that is better? No.
    That is an ignorant view. The client language provides a toolbox, where each tool has a specific application. The knowledgeable developer will use the right tool for the job. The idiot developer will select one tool and use it as The Hammer to "solve" all the problems.

  • Help needed in Ref cursor

    Hi,
    I am new to Ref Cursor concepts and I am trying a small block but its throwing error. Pls help me.
    PACKAGE SPEC:
    CREATE OR REPLACE PACKAGE PKG_JOBINFO AS
    PROCEDURE JOBINFO ( v_job_id IN number, p_cursor OUT PKG_JOBINFO.RESULT_REF_CURSOR);
    TYPE RESULT_REF_CURSOR IS REF CURSOR;
    END PKG_JOBINFO;
    PACKAGE BODY:
    CREATE OR REPLACE package body PKG_JOBINFO
    AS
    PROCEDURE JOBINFO ( v_job_id IN number,
    p_cursor OUT PKG_JOBINFO.RESULT_REF_CURSOR)
    AS
    BEGIN
    OPEN p_cursor FOR
    SELECT JOB_ID,
    JOB_NAME,
    TABLE_NAME
    FROM JOB_INFO
    WHERE JOB_ID=V_JOB_ID;
    EXCEPTION
    WHEN OTHERS THEN
    raise;
    END;
    END;
    While compiling the package i am not getting any errors. I am getting errors only while executing
    SQL> exec PKG_JOBINFO.JOBINFO ('23');
    BEGIN PKG_JOBINFO.JOBINFO ('23'); END;
    ERROR at line 1:
    ORA-06550: line 1, column 7:
    PLS-00306: wrong number or types of arguments in call to 'JOBINFO'
    ORA-06550: line 1, column 7:
    PL/SQL: Statement ignored
    Please help me to resolve this error.
    Thanks.

    user497267 wrote:
    Thanks its working. So if we are using ref cursor we have to execute like this only.To add...
    The actual issue you were experiencing was not because you were using ref cursors specifically, but because you had an OUT parameter in your procedure.
    When you have an OUT parameter, you need to ensure that you pass in a variable to that parameter of the procedure in order that the procedure can populate it. In your case you were only passing in the first parameter, but you weren't passing in a variable to capture the OUTput.
    What Alex showed was that by declaring a variable of the same datatype (ref cursor in your case) and passing that in as the second parameter, that variable was populated with the ref cursor information from inside the procedure. Once that variable was populated, after the procedure call, the data from that ref cursor can be obtained (using SQL*Plus' print command in Alex's example).

  • Need help with Ref Cursor

    Dear All,
    Version : '11.2.0.2.0'
    I have a procedure which gets called from a screen ( on .Net) if use pushes a particular button.
    The procedure takes 15 minutes to complete.
    My requirement is to intimate the front end(.Net) as soon as the procedures gets called successfully without waiting for its completion as it is going to take 15 mins.
    The reason being, the front end will popup a message saying that " The process is going to take long time, A mail will be sent to u after its completion"  once it has been intimated that the procedure has been called successfully.
    Could you please suggest can i return a cursor to front end without even manipulating the data.
    i hope my question is understandable.

    9876564 wrote:
    Dear All,
    Version : '11.2.0.2.0'
    I have a procedure which gets called from a screen ( on .Net) if use pushes a particular button.
    The procedure takes 15 minutes to complete.
    My requirement is to intimate the front end(.Net) as soon as the procedures gets called successfully without waiting for its completion as it is going to take 15 mins.
    The reason being, the front end will popup a message saying that " The process is going to take long time, A mail will be sent to u after its completion"  once it has been intimated that the procedure has been called successfully.
    Could you please suggest can i return a cursor to front end without even manipulating the data.
    i hope my question is understandable.
    It seems to me that your problem is not an Oracle one, but a user interface one.
    I'm assuming the slowness of the procedure is due to how long it's taking to query the data, and if you can't speed that up then, to have the procedure return a ref cursor, the connection between the front end and the procedure must be maintained (the process on Oracle must belong to a session, and the front end needs to be connected to that session).
    So, you can't have Oracle go off and start a seperate process and then pass back the ref cursor when it's finished, because even though you can spawn off seperate processes such as using DBMS_JOB or DBMS_SCHEDULER, they will run in their own 'session' and the calling code won't keep a hold on any of those sessions... they're effectively stand-alone and seperate.
    What you'll have to do is likely get your .net application to spawn off a child process to the main app, that presents the message saying it'll take some time, does the call to Oracle and waits for Oracle to return the resultant ref cursor, which it can then pass back to the main application etc.  Due to .net allowing for multi-tasking, that is where the split processing should take place.

Maybe you are looking for

  • Opening and closing balance diff.In T.code FBCJ

    Hi exports, While we r checking Fbcj T.code for a day, the the closing balance of the day is difference for the next day opening balance.What is the reason behind that. Eg. In FBCJ- 30.3.2010       closing balance is      46,888.00/- 31.3.2010      

  • Error when renaming a query

    Error Message: Cannot access a disposed object. Object name: 'EditorDialog'. Stack Trace: System.ObjectDisposedException: Cannot access a disposed object. Object name: 'EditorDialog'.    at System.Windows.Forms.Control.CreateHandle()    at System.Win

  • RSS implementation: error  MM_XSLTransform error.

    I'm new to this forum so please forgive in advance any dumb questions.  I am using CS3 trying to post RSS feeds on my web site from an external xml source.  I have configured a testing server running Suse 11.1 / Apache/ PHP 5.2 for development purpos

  • Should object CO_PROCESS be archived?

    Hi gurus, I need to archive my whole company codes transaction data for the previous years. Please guide me should CO_PROCESS be archived. As per my knowledge, EC_PCA_ITM, COPA2_xxxx, CO_ITEM, CO_COPC, CO_ORDER and CO_TOTAL should be archived. Please

  • WLS 7.0 sp2 - Servlet Problems with SOAP messages

              I'm using Weblogic 7.0 SP2 and trying to create a Servlet to receive SOAP wrapped           XML messages. I'm getting the following error. Is this a problem with WLS7.0sp2's           support of JAXM? The System.out.println's indicate I hav