Sys_refcursor rowtype

BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE     11.2.0.2.0     Production
TNS for HPUX: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Productionthis works fine.
DECLARE
   myrefcursor   SYS_REFCURSOR;
   TYPE myrecord IS RECORD
      mydate     DATE,
      mynumber   NUMBER
   TYPE mytable IS TABLE OF myrecord;
   mylist        mytable;
BEGIN
   OPEN myrefcursor FOR
          SELECT SYSDATE + LEVEL mydate, LEVEL mynumber
            FROM DUAL
      CONNECT BY LEVEL <= 10;
   FETCH myrefcursor
   BULK COLLECT INTO mylist;
   CLOSE myrefcursor;
   FOR indx IN mylist.FIRST .. mylist.LAST
   LOOP
      DBMS_OUTPUT.put_line (
            'indx: '
         || indx
         || ' mydate: '
         || mylist (indx).mydate
         || ' mynumber: '
         || mylist (indx).mynumber);
   END LOOP;
END;
indx: 1 mydate: 25-MAY-12 mynumber: 1
indx: 2 mydate: 26-MAY-12 mynumber: 2
indx: 3 mydate: 27-MAY-12 mynumber: 3
indx: 4 mydate: 28-MAY-12 mynumber: 4
indx: 5 mydate: 29-MAY-12 mynumber: 5
indx: 6 mydate: 30-MAY-12 mynumber: 6
indx: 7 mydate: 31-MAY-12 mynumber: 7
indx: 8 mydate: 01-JUN-12 mynumber: 8
indx: 9 mydate: 02-JUN-12 mynumber: 9
indx: 10 mydate: 03-JUN-12 mynumber: 10however if I attempt to define mytable as a table of myrefcursor%rowtype it becomes upset.
DECLARE
   myrefcursor   SYS_REFCURSOR;
   TYPE mytable IS TABLE OF myrefcursor%ROWTYPE; -- this dog don't hunt
   mylist        mytable;
BEGIN
   OPEN myrefcursor FOR
          SELECT SYSDATE + LEVEL mydate, LEVEL mynumber
            FROM DUAL
      CONNECT BY LEVEL <= 10;
   FETCH myrefcursor
   BULK COLLECT INTO mylist;
   CLOSE myrefcursor;
   FOR indx IN mylist.FIRST .. mylist.LAST
   LOOP
      DBMS_OUTPUT.put_line (
            'indx: '
         || indx
         || ' mydate: '
         || mylist (indx).mydate
         || ' mynumber: '
         || mylist (indx).mynumber);
   END LOOP;
END;
ORA-06550: line 6, column 29:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 6, column 4:
PL/SQL: Item ignored
ORA-06550: line 16, column 22:
PLS-00597: expression 'MYLIST' in the INTO list is of wrong type
ORA-06550: line 15, column 4:
PL/SQL: SQL Statement ignored
ORA-06550: line 26, column 27:
PLS-00487: Invalid reference to variable 'MYREFCURSOR%ROWTYPE'
ORA-06550: line 22, column 7:
PL/SQL: Statement ignoredI was reading the documentation but my tiny little brain is having a hard time understanding why this construct does not work.
is there a way to do this?
I only ask because my real cursor/s select alot of stuff and it is a pain to create a record for each sys_refcursor

hi
error throwing because when you declare
TYPE mytable IS TABLE OF myrefcursor%ROWTYPE; -- this dog don't huntcompiler do not know about myrefcursor anything except type;
you need use simple cursor
declare
     cursor c1  is
         select * from dual;
     r1 c1%rowtype;
begin
     open c1;
     fetch c1
         into r1;
     close c1;
     dbms_output.put_line(r1.dummy);
end;good luck

Similar Messages

  • Convert %rowtype function return into sys_refcursor

    I have an interesting problem. We have created a standard pattern for all of our "get" functions for returning data to always return a sys_refcursor to our JAVA application as it is very easy to deal with cursors and convert them to Java Collection objects (i.e. List). I'm having an issue though with the fact that some of our lower level (DML layer) pl sql code returns [TABLE_NAME]%rowtype types. I haven't found a way for jdbc to handle this type of structure, and have found some people saying that is just can't (which makes sense).
    What I would like to pose is if anyone has a clever way to manually create a cursor and insert the %rowtype object into it. It sounds simple enough, but I've tried a few approaches and nothing works. Maybe I'm just too tired or it's simply not possible.
    Any help would be greatly appreciated.

    You could use pipelined functions, something like that:
    create table testtable
       pk number,
       col2 varchar2(10)
    insert into testtable values(1, 'ABC');
    create or replace package testpack
    as
       type tabtype_testtable is table of testtable%rowtype;
       function f_get_row return testtable%rowtype;
       function f_pipe_row return tabtype_testtable pipelined;
       function f_ret_refcur return sys_refcursor;
    end testpack;
    create or replace package body testpack
    as
       function f_get_row return testtable%rowtype
       is
          r_testtable testtable%rowtype;
       begin
          select pk,
                 col2
          into   r_testtable
          from   testtable
          where  rownum = 1
          return(r_testtable);
       end f_get_row;
       function f_pipe_row return tabtype_testtable pipelined
       is
       begin
          pipe row(f_get_row);
          return;
       end f_pipe_row;
       function f_ret_refcur return sys_refcursor
       is
          r_refcur sys_refcursor;
       begin
          open r_refcur for
             select * from table(f_pipe_row);
          return(r_refcur);
          close r_refcur;
       end f_ret_refcur;
    end;
    /Regards,
    Gerd
    Message was edited by:
    gerd_99
    Forgot to close the cursor in f_ret_refcur.

  • How to use  SYS_REFCURSOR as a table  in calling sp

    hi gurus,
    here i have 2 sp in which i m calling GET_OPREF inside GET_REFCALL. and in GET_REFCAL i want to use output cursou as a table .
    plz help me ....
    CREATE OR REPLACE PROCEDURE GET_OPREF
    (p_cursor OUT  SYS_REFCURSOR )
    is
    begin
    open p_cursor FOR
        select 10 amt from dual union all
        select  20 amt from dual union all
        select 30 amt from dual ;
    end GET_OPREF ;
    CREATE OR REPLACE PROCEDURE GET_REFCALL
    is
      c_cursor   SYS_REFCURSOR ;
      r_emp      get_2%rowtype ;
       v_tot int ;
    begin 
      GET_OPREF(c_Cursor);
    select sum (amt) into v_tot from c_Cursor;  -- *here i want to user cursor as a table*
        dbms_output.put_line(v_tot ); 
    end get_refcall ;Edited by: user12108669 on Dec 1, 2009 5:03 AM
    Edited by: user12108669 on Dec 1, 2009 5:09 AM

    After you run the procedure your cursor is open. You can fetch it right away and use it like you would use a regular cursor.
    SQL> set serveroutput on
    SQL> CREATE OR REPLACE PACKAGE type_pack AS
      2  type t_cur is ref cursor;
      3  type t_tab is table of number;
      4  END type_pack;
      5  /
    Package created.
    SQL> CREATE OR REPLACE procedure test_ref(cur_output out sys_refcursor) i
      2  begin
      3    open cur_output for
      4    with test as
      5      (select 1 num from dual
      6       union select 2 num from dual
      7       union select 3 num from dual)
      8      select num
      9        from test;
    10  end;
    11  /
    Procedure created.
    SQL> CREATE OR REPLACE procedure run_test is
      2    cur_test sys_refcursor;
      3    tab_cur type_pack.t_tab;
      4  begin
      5    dbms_output.put_line('running...');
      6    test_ref(cur_test);
      7    fetch cur_test bulk collect
      8      into tab_cur;
      9    for i in 1 .. tab_cur.count
    10    loop
    11    dbms_output.put_line(tab_cur(i));
    12    end loop;
    13  end;
    14  /
    Procedure created.
    SQL> exec run_test;
    running...
    1
    2
    3
    PL/SQL procedure successfully completed.
    SQL>

  • How to get rowtype of a ref cursor?

    Hello again,
    I need your help once more.
    I have a function in a package which returns a SYS_REFCURSOR. Now I am trying to open that cursor from the application (currently sql_developer) to show the data. For starting I have a simple query in the statement: select * from emp;
    Later it will be a complex query which returns results of multiple joined tables.
    I have the following package:
    >
    CREATE OR REPLACE
    PACKAGE P_TEST_ASP AS
    FUNCTION test_asp_1 (dti_id in NUMBER)
    RETURN SYS_REFCURSOR;
    END P_TEST_ASP;
    >
    And here comes the package body:
    >
    CREATE OR REPLACE
    PACKAGE BODY P_TEST_ASP AS
    FUNCTION test_asp_1 (dti_id in NUMBER)
    RETURN SYS_REFCURSOR AS
    resultCursor SYS_REFCURSOR;
    BEGIN
    OPEN resultCursor FOR select * from emp;
    RETURN resultCursor;
    END test_asp_1;
    END P_TEST_ASP;
    >
    Now my Problem is that I do not know how to get the cursor's rowtype to assign it to the collection:
    I Managed to access the cursor when I set the rowtype of my collection res to emp%rowtype.
    >
    set serveroutput on
    DECLARE
    type        testtype is TABLE OF emp%rowtype;
    myCursor SYS_REFCURSOR;
    res testtype;
    BEGIN
    -- Get ref cursor from function
    myCursor := p_test_asp.test_asp_1(9);
    FETCH myCursor BULK COLLECT into res;
    dbms_output.put_line('Value from cursor:' );
    CLOSE myCursor;
    END;
    >
    But that will not work with the final statement.
    I would like to set the rowtype to myCursor%rowtype -> But I don't know how to do it:
    >
    set serveroutput on
    DECLARE
    type        testtype is TABLE OF myCursor%rowtype;
    myCursor SYS_REFCURSOR;
    res testtype;
    BEGIN
    -- Get ref cursor from function
    myCursor := p_test_asp.test_asp_1(9);
    FETCH myCursor BULK COLLECT into res;
    dbms_output.put_line('Value from cursor:' );
    CLOSE myCursor;
    END;
    >
    this statement throws the following errors:
    Fehlerbericht:
    ORA-06550: line 2, column 36:
    PLS-00320: the declaration of the type of this expression is incomplete or malformed
    ORA-06550: line 2, column 3:
    PL/SQL: Item ignored
    ORA-06550: line 11, column 36:
    PLS-00597: expression 'RES' in the INTO list is of wrong type
    ORA-06550: line 11, column 3:
    PL/SQL: SQL Statement ignored
    06550. 00000 - "line %s, column %s:\n%s"
    *Cause:    Usually a PL/SQL compilation error.
    Edited by: Andreas S. on 13.12.2011 04:56

    Why do you want to use a collection? I dont see any need for that.
    Lets say your client is SQL Plus. SQL Plus provides a API called PRINT that prints the cursor. You can use that.
    SQL> var rc refcursor
    SQL>
    SQL> exec open :rc for select level from dual connect by level <= 10;
    PL/SQL procedure successfully completed.
    SQL> print rc
         LEVEL
             1
             2
             3
             4
             5
             6
             7
             8
             9
            10
    10 rows selected.
    SQL>Like wise whatever is your client use the correct API that offers to handle cursors.

  • Sql greater than 32k in sys_refcursor

    hi
    We have to write function/procedure to form reports in java there we just bind the record sets into grid. We have to return the record set as SYS_REFCURSOR.
    Most of the sqls are involved in pivoting and so query length is beat 32k.
    Is there any way to pass the large sql to ref cursor?
        CREATE OR REPLACE FUNCTION TESTFUN (pREFCUR OUT SYS_REFCURSOR) RETURN NUMBER
        IS
            vSQLSTR VARCHAR2(32000);
            nRETURN NUMBER;
        BEGIN
            vSQLSTR := ' SQL > 32K ';
            OPEN pREFCUR FOR vSQLSTR;
                RETURN 1;
            EXCEPTION
                WHEN OTHERS THEN
                    COMMONEXCEPTIONHANDLER(SQLCODE, SQLERRM); -- log errors which is having RAISE_APPLICATION_ERROR
                    RETURN -1;
        END;version
    SQL> select * from v$version;
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
    PL/SQL Release 10.2.0.1.0 - Production
    CORE    10.2.0.1.0      Production
    TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - ProductionAdded Version details
    Edited by: knksoft on Jun 7, 2013 6:44 AM

    >
    using DBMS_SQL i can parse and execute sqls. But i have to return my output by refcursor. If it is PLSql limit, is there any work around available?
    >
    The only option I can think of for your Oracle version is:
    1. create a pipelined function that uses DBMS_SQL to create rows for the result set. Here is a sample PIPELINED function; yours would gather the rows using DBMS_SQL.
    CREATE OR REPLACE function get_emp_data
      return emp_table_type
      PIPELINED
      as
       TYPE EmpCurTyp IS REF CURSOR RETURN emp%ROWTYPE;
        emp_cv EmpCurTyp;
        l_rec  emp%rowtype;
      begin
        open emp_cv for select * from emp;
        loop
          fetch emp_cv into l_rec;
          exit when (emp_cv%notfound);
          pipe row( emp_scalar_type( l_rec.empno, LOWER(l_rec.ename),
              l_rec.job, l_rec.mgr, l_rec.hiredate, l_rec.sal, l_rec.comm, l_rec.deptno ) );
        end loop;
        return;
      end;
    / 2. create a simple procedure that opens and returns a REF CURSOR on a call to that pipelined procedure
    OPEN myCursor FOR SELECT * FROM TABLE(get_emp_data);That 'myCursor' would be the REF CURSOR that gets returned to the client.

  • Use of Sys_Refcursor in Oracle Forms10g...

    Hi ,
    I test the routines found in http://sheikyerbouti.developpez.com/recordset/secord_set.htm
    I have a problem in the sample routine in Forms10g regarding sys_refcursor...
    Whereas , the whole routine database part and client side in SQL*PLUS works fine.... the client side on Forms10g does not.......
    The error message ORA-01001 appears....(I havent't found this type of error).
    I have transformed the client side block in Forms10g as follows:
    Declare
          cur SYS_REFCURSOR ;
       rec EMP%ROWTYPE ;
      begin
            cur := Return_Cursor( 'SELECT * FROM EMP WHERE DEPTNO=10' ) ;
            loop
                    fetch cur into rec;
                    exit when cur%NOTFOUND;
                 :EMP.EMPNO:=rec.empno;
                 :EMP.ENAME:=rec.ename;
                 NEXT_RECORD;
            end loop ;
            close cur;
         end;So,what is wrong with this pl/sql block...????
    Thanks a lot
    Simon

    ORA-01001 - Invalid cursor. I'm not sure which version of PL/SQL Forms10g is using, but it could be lower than server side PL/SQL.
    Try to use static ref cursor. In your code replace
    cur := Return_Cursor( 'SELECT * FROM EMP WHERE DEPTNO=10' ) ;
    with
    open cur for SELECT * FROM EMP WHERE DEPTNO=10;

  • How to call a procedure with SYS_REFCURSOR OUT parameter

    Hi,
    Using Oracle 11g R2.
    I'd like to know if it is possible to display the results of a SYS_REFCURSOR in a query. For example, if I had the following stored procedure
    create or replace procedure testprocedure (result OUT sys_refcursor)
    as
    begin
       open result for
          select 1 from dual
          union all
          select 2 from dual;
    end;
    I'd like to call this procedure similar to the way a query is called and executed. Like this
    select * from testprocedure
    I've seen plenty of examples on the web which show how it is possible to loop through results of a sys_refcursor inside of an anonymous block and display the results using dbms_output.putline, but this isn't the method I am looking for.

    I'd like to know if it is possible to display the results of a SYS_REFCURSOR in a query. For example, if I had the following stored procedure
    No - you can only use schema object types (SQL) in SQL queries and only then if you call a function.
    The function can return a SQL collection type or it can be a PIPELINED function whose return value is a SQL collection type. Either way your query will use the TABLE function and be of the form:
    select * from TABLE(testfunction);
    This is sample code for a PIPELINED function based on the SCOTT.EMP table. The function takes a department number parameter and returns the EMP rows for that department:
    -- type to match emp record
    create or replace type emp_scalar_type as object
      (EMPNO NUMBER(4) ,
       ENAME VARCHAR2(10),
       JOB VARCHAR2(9),
       MGR NUMBER(4),
       HIREDATE DATE,
       SAL NUMBER(7, 2),
       COMM NUMBER(7, 2),
       DEPTNO NUMBER(2)
    -- table of emp records
    create or replace type emp_table_type as table of emp_scalar_type
    -- pipelined function
    create or replace function get_emp( p_deptno in number )
      return emp_table_type
      PIPELINED
      as
       TYPE EmpCurTyp IS REF CURSOR RETURN emp%ROWTYPE;
        emp_cv EmpCurTyp;
        l_rec  emp%rowtype;
      begin
        open emp_cv for select * from emp where deptno = p_deptno;
        loop
          fetch emp_cv into l_rec;
          exit when (emp_cv%notfound);
          pipe row( emp_scalar_type( l_rec.empno, LOWER(l_rec.ename),
              l_rec.job, l_rec.mgr, l_rec.hiredate, l_rec.sal, l_rec.comm, l_rec.deptno ) );
        end loop;
        return;
      end;
    select * from table(get_emp(20))

  • Problem in Creating %rowtype records - Help Needed...

    Hi,
    I have created some base view called...
    CREATE OR REPLACE VIEW TEST AS SELECT SYSDATE FROM DUAL;
    Then i am using this view object in the function for creating row type by recreating/replacing the view by the parameter "tablename - tname".
    CREATE OR REPLACE FUNCTION abc(rs in sys_refcursor,tname varchar2 as ) return number is
    PROCEDURE CALLPROC IS
    TYPE RC IS test%rowtype index by binary_integer;
    rc1 rc;
    begin
    fetch rs bulk collect into rc1;
    end;
    begin
    Execute immediate 'create or replace view test as select * from '||tname;
    dbms_output.put_line ('Success');
    call_proc;
    return (1);
    exception
    return (-1);
    end;
    when i am executing this package
    declare
    x number;
    rc is sys_refcursor;
    begin
    open rc for select eno,ename,mgr,sal from emp;
    x:=abc(rc,'EMP');
    end;
    error:
    ====
    invalid fetch arguments list error
    Please help me out how could i trigger the record type (rowtype) object based on the passing parameters (table name).
    Thanks in advance...

    Why are you dynamically changing the table the view selects from? This seems like you are asking for trouble by forcing objects that reference view Test to go invalid when you change it. What is the purpose of the BULK COLLECT within CALLPROC? The results that are returned into rc1 are lost once that inner procedure is finished.
    What you trying to accomplish?

  • Pass a rowtype to a function in sql

    Hi all,
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0
    Can you please tell me if the following is possible:
    I have a function that takes a rowtype as one of the parameters and returns a varchar2
    function test( pa_text in varchar2,
                   pa_emp  in employee%rowtype
      ) return varchar2I had hoped I could use this function in a implicit cursor, something like
    begin
      for c in ( select d.deptno
                 ,      test(d.deptno, e.*) test
                 from   emp  e
                 ,      dept d
                 where  e.deptno = d.deptno )
      loop
      end loop;
    end;But this results in
    Error: PL/SQL: ORA-01747: invalid user.table.column, table.column, or column specificationIs something like this even possible?
    Thanks!
    Rob

    What you could do:
    declare
        v_deptno dept.deptno%type;
        v_dcur sys_refcursor;
        v_ecur sys_refcursor;
        v_emp emp%rowtype;
    begin
        open v_dcur for select  d.deptno,
                                cursor(
                                       select  e.*
                                         from  emp e
                                         where e.deptno = d.deptno
                                      ) e
                          from  dept d;
        loop
          fetch v_dcur
            into v_deptno,
                 v_ecur;
            exit when v_dcur%notfound;
          loop
            fetch v_ecur
              into v_emp;
            exit when v_ecur%notfound;
            test(v_deptno,v_emp);
          end loop;
          close v_ecur;
        end loop;
        close v_dcur;
    end;
    /For example:
    SQL> create or replace
      2    function test(
      3                  pa_text in varchar2,
      4                  pa_emp  in emp%rowtype
      5                 )
      6      return varchar2
      7      is
      8      begin
      9          return 'DEPTNO = ' || pa_text || ' ENAME = ' || pa_emp.ename;
    10  end;
    11  /
    Function created.
    SQL> set serveroutput on
    SQL> declare
      2      v_deptno dept.deptno%type;
      3      v_dcur sys_refcursor;
      4      v_ecur sys_refcursor;
      5      v_emp emp%rowtype;
      6  begin
      7      open v_dcur for select  d.deptno,
      8                              cursor(
      9                                     select  e.*
    10                                       from  emp e
    11                                       where e.deptno = d.deptno
    12                                    ) e
    13                        from  dept d;
    14      loop
    15        fetch v_dcur
    16          into v_deptno,
    17               v_ecur;
    18          exit when v_dcur%notfound;
    19        loop
    20          fetch v_ecur
    21            into v_emp;
    22          exit when v_ecur%notfound;
    23          dbms_output.put_line(test(v_deptno,v_emp));
    24        end loop;
    25        close v_ecur;
    26      end loop;
    27      close v_dcur;
    28  end;
    29  /
    DEPTNO = 10 ENAME = CLARK
    DEPTNO = 10 ENAME = KING
    DEPTNO = 10 ENAME = MILLER
    DEPTNO = 20 ENAME = SMITH
    DEPTNO = 20 ENAME = JONES
    DEPTNO = 20 ENAME = SCOTT
    DEPTNO = 20 ENAME = ADAMS
    DEPTNO = 20 ENAME = FORD
    DEPTNO = 30 ENAME = ALLEN
    DEPTNO = 30 ENAME = WARD
    DEPTNO = 30 ENAME = MARTIN
    DEPTNO = 30 ENAME = BLAKE
    DEPTNO = 30 ENAME = TURNER
    DEPTNO = 30 ENAME = JAMES
    PL/SQL procedure successfully completed.
    SQL> SY.

  • Sys_refcursor vs collection parameter

    When providing a result set as a function parameter, are there any advantages/disadvantages to using a sys_refcursor vs a user-defined collection type?

    >
    I was planning on iterating through the ref cursor and constructing a collection, then using table() to include it in the JOIN. Is that approach viable?
    >
    No - Justin already told you to use a pipelined function. The function can be treated as a table just like you want.
    Try this samle code in the SCOTT schema.
    -- type to match emp record
    create or replace type emp_scalar_type as object
      (EMPNO NUMBER(4) ,
       ENAME VARCHAR2(10),
       JOB VARCHAR2(9),
       MGR NUMBER(4),
       HIREDATE DATE,
       SAL NUMBER(7, 2),
       COMM NUMBER(7, 2),
       DEPTNO NUMBER(2)
    -- table of emp records
    create or replace type emp_table_type as table of emp_scalar_type
    -- pipelined function
    create or replace function get_emp( p_deptno in number )
      return emp_table_type
      PIPELINED
      as
       TYPE EmpCurTyp IS REF CURSOR RETURN emp%ROWTYPE;
        emp_cv EmpCurTyp;
        l_rec  emp%rowtype;
      begin
        open emp_cv for select * from emp where deptno = p_deptno;
        loop
          fetch emp_cv into l_rec;
          exit when (emp_cv%notfound);
          pipe row( emp_scalar_type( l_rec.empno, LOWER(l_rec.ename),
              l_rec.job, l_rec.mgr, l_rec.hiredate, l_rec.sal, l_rec.comm, l_rec.deptno ) );
        end loop;
        return;
      end;
    select * from table(get_emp(20))Now you can join that 'table' to the dept table
    SQL> select * from dept where deptno in (select deptno from table(get_emp(20)));
        DEPTNO DNAME          LOC
            20 RESEARCH       DALLAS
    SQL>Isn't that what you said you wanted to do?

  • Sys_refcursor to dynamic record type

    Hi there,
    I had a procedure A that is calling procedure B to return the sys_refcursor. And procedure A will using the data that return from cursor procedure B for some processing. Is there anyway that i don't need to fix the record column by calling <record name> <cursor name>%RowType? Because I had an error if i declare the record with the cursor type that not yet open. Example of the code as below.
    Procedure ProcA(a1 Integer default Null) Is
    cursor1 sys_refcursor;
    record1 cursor1%RowType;
    Begin
    ProcB(0, cursor1);
    If cursor1%Isopen Then
    Loop
    Fetch cursor1
    Into record1;
    Exit When cursor1%NotFound;
    End Loop;
    Close cursor1;
    End If;
    End;
    Thanks in advance.

    Yes, two methods comes to mind.
    The complex, and more "correct", method is to create a DBMS_SQL cursor instead. Such a cursor has a describe interface that allows the receiver to dynamically determine the cursor's number of columns, data types of the the columns and so on.
    DBMS_SQL is documented in the Oracle® Database PL/SQL Packages and Types Reference guide.
    The second method is easier and flexible, but a bit of a hack as it can only deal with data types that can be implicitly converted to varchar2. It also requires the SELECT statement to call a type constructor using columns as properties for the constructor. The following code illustrates this concept.
    SQL> create or replace type TStrings is table of varchar2(4000);
    2 /
    Type created.
    SQL>
    SQL>
    SQL> create or replace procedure Cursor1( c IN OUT sys_refcursor ) is
    2 begin
    3 open c for
    4 select TStrings( object_id, object_name, object_type ) from all_objects;
    5 end;
    6 /
    Procedure created.
    SQL>
    SQL>
    SQL> create or replace procedure Cursor2( c IN OUT sys_refcursor ) is
    2 begin
    3 open c for
    4 select TStrings( rownum, username ) from all_users;
    5 end;
    6 /
    Procedure created.
    SQL>
    SQL>
    SQL>
    SQL> declare
    2 c sys_refcursor;
    3 cols TStrings;
    4
    5 procedure W( cLine varchar2 ) is
    6 begin
    7 DBMS_OUTPUT.put_line( cLine );
    8 exception when OTHERS then
    9 NULL;
    10 end;
    11
    12 procedure DisplayColumns( s TStrings ) is
    13 begin
    14 for i in 1..s.Count
    15 loop
    16 W( 'column '||i||' ['||s(i)||']' );
    17 end loop;
    18 end;
    19
    20 begin
    21 W( '************' );
    22
    23 W( 'opening cursor 1' );
    24 Cursor1( c );
    25 -- fetch only 1 row to demonstrate
    26 fetch c into cols;
    27 W( 'cursor has '||cols.Count||' columns' );
    28 DisplayColumns( cols );
    29 close c;
    30
    31 W( '************' );
    32
    33 W( 'opening cursor 2' );
    34 Cursor2( c );
    35 fetch c into cols;
    36 W( 'cursor has '||cols.Count||' columns' );
    37 DisplayColumns( cols );
    38 close c;
    39
    40 W( '************' );
    41 end;
    42 /
    opening cursor 1
    cursor has 3 columns
    column 1 [258]
    column 2 [DUAL]
    column 3 [TABLE]
    opening cursor 2
    cursor has 2 columns
    column 1 [1]
    column 2 [RMAN]
    PL/SQL procedure successfully completed.
    SQL>

  • WHEN TO USE SYS_REFCURSOR AND WHEN NOT TO USE REFCURSOR

    SYS_REFCURSOR is a Weakly Defined Cursor
    DECLARE emp_refcur SYS_REFCURSOR;
    when this is so simple
    why this
    DECLARE TYPE emp_cur_type IS REF CURSOR RETURN emp%ROWTYPE; my_rec emp_cur_type; ?
    Can anyone explain in what situations we should prefer among these ..when both can give same result set .

    > WHEN TO USE SYS_REFCURSOR AND WHEN NOT TO USE REFCURSOR
    When the requirements define that one is better suited than the other.
    There are no hard and fast rules about when to use a specific tool. There are however basic concepts of what the purpose of the tool is, and what typical problems the tool can address.
    Ref cursors in PL/SQL.. usually not the best of ideas. Ref cursors are designed for client-server processing.
    1) The client makes a request for data/information.
    2) PL applies the back-end server logic and construct a SQL statement for the client.
    3) PL passes the SQL to the SQL engine where it is parsed and a SQL cursor created.
    4) PL passes a reference handle to SQL cursor back to the client
    5) The client can now fetch the rows output from the cursor.
    Advantages.
    - The client does not need to know SQL, db structures, business logic, etc
    - The PL code can be improved, modified, new business logic added, without even touching a single byte of client code
    So what are the advantages where the "client" in the above is another PL/SQL program? None really.
    A "real" client (an external one like Java, C#, Delphi, etc) can receive the ref cursor handle and dynamically at run-time determine the projection (output) of the cursor.
    PL code cannot (unless using the latest 11g features). In PL code, a ref cursor is actually implemented as a DBMS_SQL cursor. A DBMS_SQL cursor is to PL code what ref cursor is to Java/C#/etc.
    So whether you define a strongly typed or weakly typed ref cursor in PL/SQL code... typical response is "so what?" as ref cursors are not really intended for PL code. Not that there are exceptions - they do come in handy at times.. but as exceptions to the rule.
    So my response is "so what?" - it is more important to *correctly" chose what type of client cursor data type you want to use to interact with the back-end SQL cursor. Once that decision has been made (based on technical sound logic), then one can look at  (minor) issues such as weak vs. strong ref cursors.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Sys_refcursor & ref cursor

    hi ,
    oracle 10.2.0.1.0
    whats the implementation difference between sys_refcursor and ref cursor ? when should one use these ? apart from knowing the fact that sys_refcursor is pre-determined weak cursor only and ref cursor could be strong or weak .
    please help .

    Simma wrote:
    Hi,
    These both are the cursor but the REF CURSOR is more restricted one, you can't return as the cursor back as we think.
    For example,
    TYPE Cursor_Ref IS REF CURSOR RETURN temp_table%ROWTYPE;
    Since the Cursor_Ref is defined as the REF CURSOR , we need to use only the colmns / fields only same as temp_table.
    As we have restricted to use temp_table%ROWTYPE;
    but in SYS_REFCURSOR, we are not restricting to any of the ROWTYPE, so it is not more restricted,
    we can return as we want.
    Edited by: Simma on Jan 11, 2011 9:22 PMYou can just writeTYPE Cursor_Ref IS REF CURSOR;
    v_cur Cursor_Ref;and this is the same asv_cur SYS_REFCURSOR;

  • Error when fetching into a variable of ROWTYPE

    I came across with a vied error when creating the body of a method.
    Problem:
    I have a Type called Emp_typ and a corresponding table Emp_tab.
    When I create the body of get method it gives the following error msg :
    Compilation errors for PACKAGE BODY CHAMITH.EMP_API
    Error: PLS-00386: type mismatch found at 'EMP_ROW' between FETCH cursor and INTO variables
    Line: 9
    Text: fetch get_emp_rec into emp_row;
    Get Method body :
    function get(id_ number) return emp_tab%rowtype IS
    cursor get_emp_rec IS
    select * from emp_tab;
    emp_row emp_tab%rowtype;
    begin
    open get_emp_rec;
    fetch get_emp_rec into emp_row;
    close get_emp_rec;
    return emp_row;
    end;
    However when I do the same without having a type upon the table emp_tab, it works fine.
    Pls give me a solution for that.
    Thanks,
    Chamith

    Hi,
    Let me know the Oracle Database Version.
    Regards,
    Sailaja

  • Is there any provision to view the selected record using SYS_REFCURSOR?

    hi friends ,
    I was using SQL Server . now i am shifting to Oracle . so we are changing the Stored Procedures in SQLServer to Oracle SP's. I have given the structure of procedure given below . If possible , i want to see the output of select statement in the TOAD editor . If any body knows please help me
    CREATE OR REPLACE PROCEDURE PS_AON
    P_STATUS OUT VARCHAR2,
    P_CUR OUT SYS_REFCURSOR
    AS
    BEGIN
    OPEN P_CUR FOR
              select colum1,column2,column3 from Table 1;
    EXCEPTION
                   WHEN OTHERS THEN
                   P_STATUS:=SQLERRM;
    END;
    This is one of the model of stored procedures i am using . And the editor i am using is TOAD 7.3.0 and oracle 9i. Is there any provision to view the selected records by running this procedure in TOAD editor
    thanks & regards

    (assuming you have relatively recent version of TOAD).
    Write a small block to call the procedure (or use Toad's 'execute procedure' option) as in the example below. Note the ':' in front of 'v_cur_out'. When you run the block, TOAD will prompt you for a value / datatype for 'v_cur_out'. Ignore the value, set the datatype to 'Cursor' and click OK. The resultset (if any) will be displayed in the Data Grid window below.
    DECLARE
       v_status VARCHAR2 (32767);
    BEGIN
       ps_aon (v_status, :v_cur_out);
       DBMS_OUTPUT.PUT_LINE ('v_status => ' || v_status);
    END;
    /

Maybe you are looking for