TYPE REF CURSOR

I have two packages (please see below). A procedure from the first package (TEST1) calls a procedure in the second package (TEST2), which has an output parameter of REF CURSOR TYPE.
I am getting an error at compile time, in the calling procedure.
Can anyone please help on finding out what am I missing here?
Thank you in advance.
- Ketan Bhuptani
Here are the procedures:
(1)
CREATE OR REPLACE PACKAGE TEST1
AS
TYPE cursor_type_pass IS REF CURSOR;
PROCEDURE call_cursor (result_flag OUT varchar2);
END TEST1;
CREATE OR REPLACE PACKAGE BODY TEST1
AS
PROCEDURE call_cursor (result_flag OUT varchar2) IS
v_salary int;
proc_cursor cursor_type_pass;
CURSOR emp_cur is select empid from emp;
BEGIN
FOR emp_cur_var IN emp_cur
LOOP
test2.open_cursor(emp_cur_var.empid, proc_cursor);
v_salary := proc_cursor.salary;
     -- getting an error Invalid reference to variable proc_cursor at this line
END LOOP;
END call_cursor;
END TEST1;
(2)
CREATE OR REPLACE PACKAGE TEST2
AS
TYPE cursor_type_return IS REF CURSOR;
PROCEDURE open_cursor (emp_id IN varchar2, out_cur OUT cursor_type_return);
END TEST2;
CREATE OR REPLACE PACKAGE BODY TEST2
AS
PROCEDURE open_cursor (emp_id IN varchar2, out_cur OUT cursor_type_return) IS
BEGIN
OPEN out_cur FOR
select location, salary from emp_information where empid = emp_id;
END open_cursor;
END TEST2;
create table emp_information
(location varchar2(30), salary int, empid varchar2(10));
create table emp
(empid varchar2(10));

Your scenario is not very clear to me, but let us do some test coding here, see if it helps you,SQL> create or replace package test_pkg1 as
  2  TYPE cursor_type1 IS REF CURSOR;
  3  procedure proc1;
  4  end;
  5  /
Package created.
SQL> create or replace package test_pkg2 as
  2  TYPE cursor_type2 IS REF CURSOR;
  3  procedure proc2(pCur IN OUT cursor_type2);
  4  end;
  5  /
Package created.
SQL> create or replace package body test_pkg2 as
  2  procedure proc2(pCur IN OUT cursor_type2) is
  3  begin
  4      open pCur for SELECT ename from  my_emp;
  5  end;
  6  end;
  7  /
Package body created.
SQL> create or replace package body test_pkg1 as
  2  procedure proc1 is
  3  vRefCur    cursor_type1;
  4  vEname     VARCHAR2(20);
  5  vRefCur2   test_pkg2.cursor_type2;
  6  begin
  7    test_pkg2.proc2(vRefCur); -- this is possible, but I do not like it.
  8    vRefCur2 := vRefCur; -- you can also do this, but you have no reason to do this
  9    loop
10      fetch vRefCur2 into vEname;
11      exit when vRefCur2%NOTFOUND;
12      dbms_output.put_line(vEname);
13    end loop;
14    close vRefCur2;
15  end;
16  end;
17  /
Package body created.
SQL> exec test_pkg1.proc1;
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
PL/SQL procedure successfully completed.
Let us make it simple,
SQL> create or replace package body test_pkg1 as
  2  procedure proc1 is
  3  vRefCur    cursor_type1;
  4  vEname     VARCHAR2(20);
  5  --vRefCur2   test_pkg2.cursor_type2;
  6  begin
  7    test_pkg2.proc2(vRefCur);
  8    --vRefCur2 := vRefCur;
  9    loop
10      fetch vRefCur into vEname;
11      exit when vRefCur%NOTFOUND;
12      dbms_output.put_line(vEname);
13    end loop;
14    close vRefCur;
15  end;
16  end;
17  /
Package body created.
SQL> exec test_pkg1.proc1;
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
PL/SQL procedure successfully completed.You can play with it number of different ways.
Let me know what questions and we will take it from there.
Thx,
SriDHAR

Similar Messages

  • OUT parameter of type REF CURSOR

    DB: Oracle Database 10g Release 10.2.0.3.0 - Production
    OS: Windows xp
    I have got these two types:
    TYPE myrecord IS RECORD
    (column1           tab1.col1%TYPE,
    column2         tab1.col2%TYPE
    TYPE my_ref_cur IS REF CURSOR RETURN myrecord;Then I have this procedure:
    PROCEDURE proc1 (pid          IN       NUMBER, pout1      OUT my_ref_cur);How can I call the procedure from a pl/sql block and fetch from the cursor variable?
    I tried this, but it does not work for me:
    DECLARE
      lid NUMBER := 748;
      lcur1 my_ref_cur
    begin
      proc1 (lid, lcur1);
    end;I receive the error Wrong number or types of arguments in call to 'proc1'
    How it must be called?
    Thanks

    seems to work
    SQL> create table tab1
      2  (col1 number
      3  ,col2 number
      4  );
    Table created.
    SQL> insert into tab1 values (1,1);
    1 row created.
    SQL> commit;
    Commit complete.
    SQL>
    SQL> create or replace package pack
      2  is
      3     TYPE myrecord IS RECORD
      4     (column1           tab1.col1%TYPE,
      5      column2         tab1.col2%TYPE
      6     );
      7     TYPE my_ref_cur IS REF CURSOR RETURN myrecord;
      8     procedure proc1 (pid          IN       number
      9                    , pout1      OUT my_ref_cur);
    10  end pack;
    11  /
    Package created.
    SQL>
    SQL> show error
    No errors.
    SQL>
    SQL> create or replace
      2  package body pack
      3  is
      4     procedure proc1 (pid          IN       number
      5                    , pout1      OUT my_ref_cur
      6                    )
      7     is
      8     begin
      9        open pout1
    10        for select col1
    11                 , col2
    12        from tab1;
    13     end proc1;
    14  end pack;
    15  /
    Package body created.
    SQL> show error
    No errors.
    SQL>
    SQL> DECLARE
      2    lid NUMBER := 748;
      3    lcur1 pack.my_ref_cur;
      4    r tab1%rowtype;
      5  begin
      6    pack.proc1 (lid, lcur1);
      7    fetch lcur1 into r;
      8    dbms_output.put_line (r.col1||' - '||r.col2);
      9    close lcur1;
    10  end;
    11  /
    1 - 1
    PL/SQL procedure successfully completed.

  • Passing Ref Cursor as parameter to object type method

    I am encountering a problem passing a parameter of type REF CURSOR to methods of a set of object types I am developing.
    Here is an example of what I am trying to do:
    create or replace package p1 as
    type c_Cursor is ref cursor;
    end p1;
    create or replace type t_Object as object
    not instantiable method m1(p_Cursor in p1.c_Cursor) return varchar2
    ) not instantiable not final;
    create or replace type t_Subtype under t_Object as
    overriding method m1(p_Cursor in p1.c_Cursor)
    return varchar2
    The problem is that the PL/SQL compiler gives the error message PLS-00201 "p1.c_Cursor" not defined.
    According to my PL/SQL book (SF's Oracle PL/SQL Programming) the only way to use a ref cursor as a parameter to functions/procedures is to wrap them in a package. Indeed I have developed a test procedure in a different package that uses p1.c_Cursor as a parameter and it works fine.
    Oracle's documemtation suggests that object security (roles etc) can cause this error but as all the objects are being created in the same schema I don't see how this should be a problem.
    If anyone can suggest how to get around this problem I will be very grateful.
    BTW, if there are any mistakes in my sample code it's because I am writing it from memory as I don't have Internet access at work.
    Thanks,
    Charles.

    Thanks for your reply. I am still baffled as to why it doesn't work but, as you correctly point out, SYS_REFCURSOR works just fine. I figured that out earlier today and now the problem is solved.
    Charles.

  • Ref cursor type

    Hello All,
    I have a small procedure like the following.
    CREATE OR REPLACE procedure PROC_DEPT_SALE(SALEC_CUR out sys_refcursor)
    as
    begin
    open sale for select * from dept_sale where dept_no='Z2341324';
    end;
    I just want to execute it from toad like
    exec PROC_DEPT_SALE( C1);
    In order to do that i need to have a sys_refcursor c1 created in my DB. I know that i can execute it as a plsql block like this
    DECLARE
    SALE SYS_REFCURSOR;
    BEGIN
    PROC_DEPT_SALE( SALE );
    END;
    Could anyone please let me know how can i create a TYPE REF cursor which is permanantly stored in the DB and then execute the proc the way i wanted to using the first statement.
    Thanks

    Actually, you had it correct with
    CREATE OR REPLACE package cur_type as
       TYPE ref_cur IS REF CURSOR;
    end;but you still need to declare a variable of the type to pass the the procedure since the procedure needs to have a cursor variable to open. Given your package, the call would be more like:
    DECLARE
       l_cur cur_type.ref_cur;
    BEGIN
       proc_dept_sale(l_cur);
       <do something with l_cur>
    END;Forget about the cursor for a minute and think through this example. If I have a procedure that looks like:
    CREATE PROCEDURE get_name (p_id    IN NUMBER,
                               p_name OUT VARCHAR2) AS
    BEGIN
       SELECT last_name||', '||first_name
       INTO p_name
       FROM employee
       WHERE emp_id = p_id;
    END;I can pass a literal value to the procedure for the p_id parameter (e.g. 42) or I could pass a variable holding a value.
    What do I need to pass to the procedure in p_name to get the name back out? A procedure returning a cursor is no different than this in concept.
    John

  • Calling stored proc from java using ref cursor

    Hi All,
    We have a requirement to display all the records from a table on a JAVA screen. Due to some constraints, we have to write the query in the stored proc. We are trying to return a result set from the stored proc to the java code by using a ref cursor. Please refer to the code below.
    Following is the PL/SQL proc �
    procedure sp_user_course2(v1 OUT ref_cursor, persid in varchar2) as
    begin
    open v1 for
    SELECT lrn_exp_id, crs_name FROM emp_crs
    WHERE personid = persid;
    end;
    Here ref_cursor is of TYPE REF CURSOR declared in the package specification.
    The java code is �
    Callable stmt = conn.prepareCall("call sp_user_course2(?,?)");
    stmt.registerOutParameter(1,OracleTypes.CURSOR);
    stmt.setString(2,emplId);
    stmt.execute();
    OracleCallableStatement tstmt = (OracleCallableStatement) stmt;
    ResultSet rs = tstmt.getCursor (1);
    When I run the program, I get the following error (at stmt.execute()):
    [Oracle][ODBC][Ora]ORA-06553: PLS-306: wrong number or types of arguments in call to 'SP_USER_COURSE2' 6553
    Can anyone tell me what could be the problem with this code? I have read somewhere that REF CURSOR feature is available from Oracle 9i onwards. If so, then, is there any workaround for mapping REF CURSOR to Resultsets in Oracle 8i?

    These may help
    http://www.google.co.uk/search?q=jdbc+OracleTypes.CURSOR+tutorial

  • Calling stored proc from java to return ref cursor

    Hi All,
    We have a requirement to display all the records from a table on a JAVA screen. Due to some constraints, we have to write the query in the stored proc. We are trying to return a result set from the stored proc to the java code by using a ref cursor. Please refer to the code below.
    Following is the PL/SQL proc ?
    procedure sp_user_course2(v1 OUT ref_cursor, persid in varchar2) as
    begin
    open v1 for
    SELECT lrn_exp_id, crs_name FROM emp_crs
    WHERE personid = persid;
    end;
    Here ref_cursor is of TYPE REF CURSOR declared in the package specification.
    The java code is ?
    Callable stmt = conn.prepareCall("call sp_user_course2(?,?)");
    stmt.registerOutParameter(1,OracleTypes.CURSOR);
    stmt.setString(2,emplId);
    stmt.execute();
    OracleCallableStatement tstmt = (OracleCallableStatement) stmt;
    ResultSet rs = tstmt.getCursor (1);
    When I run the program, I get the following error (at stmt.execute()):
    [Oracle][ODBC][Ora]ORA-06553: PLS-306: wrong number or types of arguments in call to 'SP_USER_COURSE2' 6553
    Can anyone tell me what could be the problem with this code? I have read somewhere that REF CURSOR feature is available from Oracle 9i onwards. If so, then, is there any workaround for mapping REF CURSOR to Resultsets in Oracle 8i?

    These may help
    http://www.google.co.uk/search?q=jdbc+OracleTypes.CURSOR+tutorial

  • Invalid SQL error using REF CURSOR

    I'm getting an "invalid SQL statement" in my function that is returning a REF CURSOR. I have tested this function in SQL*Plus and it works fine.
    -- package level variable
    TYPE t_cursor IS REF CURSOR;
    Function find_patient
         p_ssan IN varchar2,
         p_patient_details OUT t_cursor
         RETURN t_cursor
         IS
         v_cursor t_cursor;
         BEGIN
         OPEN v_cursor FOR
         SELECT name,sex,pay_grade,
              FLOOR(MONTHS_BETWEEN(sysdate,birthdate) / 12) AS age,
              patient_num
         FROM patient
         WHERE ssan = p_ssan;
         p_patient_details := v_cursor;
         RETURN p_patient_details;
         END find_patient;
    -- C# code
    OracleParameter ssan_in = new OracleParameter("p_ssan",OracleDbType.Varchar2);
    OracleParameter cursor_out = new OracleParameter("p_patient_details",OracleDbType.RefCursor);
    cmd.Parameters.Add(ssan_in).Direction = ParameterDirection.Input;
    cmd.Parameters.Add(cursor_out).Direction = ParameterDirection.Output;
    ssan_in.Value = "555555555";
    OracleRefCursor cur = (OracleRefCursor) cursor_out.Value;
    try
    dbconn.Open();
                        OracleDataReader dr = cmd.ExecuteReader();
                        while (dr.Read())
                             pat_name.Text = dr["NAME"].ToString();
                             age.Text = dr["AGE"].ToString();
                             rank.Text = dr["PAY_GRADE"].ToString();
                             gender.Text = dr["SEX"].ToString();
                             pat_num.Text = dr["PATIENT_NUM"].ToString();
    Any suggestions?

    First, you have an output parameter of type ref cursor and a return value. You shouldn't have both.
    Second, try CommandType.Text and a CommandText like:
    "begin :rc := find_patient(:ssan); end;"
    It's easier to see how to bind the parameters like that. Bind first a OracleRefCursor output parameter, then an ssan%type InputParameter.
    David

  • Dyn SQL in Ref cursor?

    Hi all,
    I have a pkg as below and its err message in compile:
    create or replace PACKAGE PKG_DARTS1 is
    TYPE TY_PARTY_DETAIL1 IS RECORD (
         PTY_TYPE xxx.yyyy%TYPE
    TYPE cur_partydetail1 IS REF CURSOR RETURN TY_PARTY_DETAIL1;
    procedure SP_GetPartyDetail1(     c_partydetail1 OUT pkg_darts1.cur_partydetail1) ;
    end PKG_DARTS1;
    create or replace PACKAGE BODY PKG_DARTS1 as
    procedure SP_GetPartyDetail1(     c_partydetail1 OUT pkg_darts1.cur_partydetail1)
    AS
         sql_stmt VARCHAR2(2000);
    BEGIN
         sql_stmt := 'select ''XXX'' FROM DUAL ';
         OPEN c_partydetail1 FOR sql_stmt;
    EXCEPTION
         WHEN OTHERS THEN
              dbms_output.put_line(sqlerrm);
    END SP_GetPartyDetail1;
    END PKG_DARTS1;
    .Warning: Package Body created with compilation errors.
    Errors for PACKAGE BODY PKG_DARTS1:
    LINE/COL ERROR
    13/3 PL/SQL: Statement ignored
    13/8 PLS-00455: cursor 'C_PARTYDETAIL1' cannot be used in dynamic SQL
    OPEN statement
    what's wrong with my syntax? anything I should care in constructing sql in ref cursor?
    thx,
    bean

    You can't use strongly types ref cursors with dynamic SQL, you will have to use weakly typed.
    Hth
    Martin

  • Returning a Ref cursor as an OUT Parameter

    Hi Guys
    I have defined 2 Types and 2 Ref cursors as shown below.I have written a procedure having 2 IN and 2 OUT parameters which are of type Ref cursors and return records to the calling client. My question is how can i test for the output of the two cursors here in pl/sql?.. also i should not close the sursors.. right?..its the job of the calling client as per my knowledge....Please suggest
    TYPE type_dept_Rec IS RECORD(deptNo varchar2);
    TYPE type_prod_Rec IS RECORD(prodType varchar2);
    TYPE deptCursor IS REF CURSOR RETURN type_dept_Rec;
    TYPE prodCursor IS REF CURSOR RETURN type_prod_Rec;
    PROCEDURE TEST (pinCode IN varchar2, prodType IN number, deptCursor OUT deptType, productCursor OUT deptType) IS
    BEGIN
    OPEN deptCursor FOR SELECT
    deptNo
    from
    DEPT
    where
    pinCode = pinCode;
    OPEN productCursor FOR SELECT
    prodCode
    from
    PROD
    where
    prodType = prodType;
    end;

    A Correction to the above code snippet
    Hi Guys
    I have defined 2 Types and 2 Ref cursors as shown below.I have written a procedure having 2 IN and 2 OUT parameters which are of type Ref cursors and return records to the calling client. My question is how can i test for the output of the two cursors here in pl/sql?.. also i should not close the sursors.. right?..its the job of the calling client as per my knowledge....Please suggest
    TYPE type_dept_Rec IS RECORD(deptNo varchar2);
    TYPE type_prod_Rec IS RECORD(prodType varchar2);
    TYPE deptCursor IS REF CURSOR RETURN type_dept_Rec;
    TYPE prodCursor IS REF CURSOR RETURN type_prod_Rec;
    PROCEDURE TEST (pinCode IN varchar2, prodType IN number, deptCursor OUT deptCursor, productCursor OUT prodCursor) IS
    BEGIN
    OPEN deptCursor FOR SELECT
    deptNo
    from
    DEPT
    where
    pinCode = pinCode;
    OPEN productCursor FOR SELECT
    prodCode
    from
    PROD
    where
    prodType = prodType;
    end

  • How return parameter ref Cursor from procedure using dynamic SQL?

    I sorry, but i very need help.
    I using Oracle 8.0.6
    I need to return parameter of type ref Cursor from procedure.
    create or replace package PlanExp is
    type cursortype is ref cursor;
    procedure ShowPlan (cursorparam out
    cursortype.............);
    end PlanExp;
    create or replace package body PlanExp is
    procedure ShowPlan (cursorparam out cursortype,
    .............) Is
    sql_str varchar2(1000);
    sql_str_select varchar2(100);
    sql_str_from varchar2(100);
    sql_str_where varchar2(500);
    Return_Code integer;
    Num_Rows integer;
    cur_id_sel integer;
    tSum_Plan DBMS_SQL.NUMBER_TABLE;
    tSum_Plan_Ch DBMS_SQL.NUMBER_TABLE;
    tSum_Plan_Day DBMS_SQL.NUMBER_TABLE;
    begin
    /* calculating string variables ........... /*
    sql_str := 'select ' || sql_str_select ||
    'from ' || sql_str_from ||
    'where ' || sql_str_where ||
    'group by ' || sql_str_select;
    cur_id_sel := dbms_sql.open_cursor;
    dbms_sql.parse(cur_id_sel, sql_str, dbms_sql.native);
    dbms_sql.define_array(cur_id_sel, 1, tSum_Plan, 20, 1);
    dbms_sql.define_array(cur_id_sel, 2, tSum_Plan_Ch, 20, 1);
    dbms_sql.define_array(cur_id_sel, 3, tSum_Plan_Day, 20, 1);
    Return_Code := dbms_sql.execute(cur_id_sel);
    delete from TEMP_SHOWPLAN;
    Loop
    Num_Rows := dbms_sql.Fetch_Rows(cur_id_sel);
    dbms_sql.column_value(cur_id_sel, 1, tSum_Plan);
    dbms_sql.column_value(cur_id_sel, 2, tSum_Plan_Ch);
    dbms_sql.column_value(cur_id_sel, 3, tSum_Plan_Day);
    if Num_Rows = 0 then
    exit;
    end if;
    Exit When Num_Rows < 20;
    End Loop;
    dbms_sql.close_cursor(cur_id_sel);
    end;
    end PlanExp;
    How return cursor (cursorparam) from 3 dbms_sql.column_value-s ?

    I am using Oracle 8.1.7, so I don't know if this will work in
    8.0.6 or not:
    SQL> CREATE TABLE test
      2    (col1                    NUMBER,
      3     col2                    NUMBER,
      4     col3                    NUMBER)
      5  /
    Table created.
    SQL> INSERT INTO test
      2  VALUES (1,1,1)
      3  /
    1 row created.
    SQL> INSERT INTO test
      2  VALUES (2,2,2)
      3  /
    1 row created.
    SQL> INSERT INTO test
      2  VALUES (3,3,3)
      3  /
    1 row created.
    SQL> CREATE TABLE temp_showplan
      2    (tSum_Plan               NUMBER,
      3     tSum_Plan_Ch            NUMBER,
      4     tSum_Plan_Day           NUMBER)
      5  /
    Table created.
    SQL> EDIT planexp
    CREATE OR REPLACE PACKAGE PlanExp
    IS
      TYPE CursorType IS REF CURSOR;
      PROCEDURE ShowPlan
        (cursorparam    IN OUT CursorType,
         sql_str_select IN     VARCHAR2,
         sql_str_from   IN     VARCHAR2,
         sql_str_where  IN     VARCHAR2);
    END PlanExp;
    CREATE OR REPLACE PACKAGE BODY PlanExp
    IS
      PROCEDURE ShowPlan
        (cursorparam    IN OUT CursorType,
         sql_str_select IN     VARCHAR2,
         sql_str_from   IN     VARCHAR2,
         sql_str_where  IN     VARCHAR2)
      IS
        sql_str                VARCHAR2 (1000);
        cur_id_sel             INTEGER;
        return_code            INTEGER;
      BEGIN
        DELETE FROM temp_showplan;
        sql_str := 'INSERT INTO   temp_showplan '
               || ' SELECT '   || sql_str_select
               || ' FROM '     || sql_str_from
               || ' WHERE '    || sql_str_where;
        cur_id_sel := DBMS_SQL.OPEN_CURSOR;
        DBMS_SQL.PARSE (cur_id_sel, sql_str, DBMS_SQL.NATIVE);
        return_code := DBMS_SQL.EXECUTE (cur_id_sel);
        DBMS_SQL.CLOSE_CURSOR (cur_id_sel);
        OPEN cursorparam FOR SELECT * FROM temp_showplan;
      END ShowPlan;
    END PlanExp;
    SQL> START planexp
    Package created.
    Package body created.
    SQL> VARIABLE g_ref REFCURSOR
    SQL> EXEC PlanExp.ShowPlan (:g_ref, 'col1, col2,
    col3', 'test', ' 1 = 1 ')
    PL/SQL procedure successfully completed.
    SQL> PRINT g_ref
    TSUM_PLAN TSUM_PLAN_CH TSUM_PLAN_DAY
             1            1             1
             2            2             2
             3            3             3

  • Using Ref cursor in Procedure.

    Hi All,
    Can i use a single ref cursor multple times within the life cycle of a procedure.
    Thanks,
    Dillip

    Yes.
    See the example here. A cursor expression is selected - repeatedly, within a loop - into emp_cur (which is of type REF CURSOR) - and emp_cur is then used to process it in a nested loop.
    (By the way - this question would be better asked in the PL/SQL).
    Regards Nigel

  • Ref -cursors usage in forms

    hi
    i have a stored procedure which accepts deptno and returns the employees from emp as an out parameter of type ref cursor.
    if i wish to see that in forms how can i get it?
    pls suggest me or if anyone has tried it kindly help with the code
    regards
    prem

    You base your block on the procedure instead of a table.
    Check "Query Data Source Type" under Database in the Property Palette
    for the block. You can select "Procedure".
    Then, in "Query Data Source Name" you specify <Package.Procedure>.
    (Alternatively you can also fill the block from a trigger by calling the procedure)
    The note 66887.1 in Metalink is an excellent cookbook.

  • Return XML from REF CURSOR

    It appears to be fairly straight-forward to get XML from a SQL query using the XML SQL Utility.
    Is it possible to use this utility (or another) to get XML from a cursor variable as well? I would like to generate overloaded get_xml functions in a PL/SQL package that will accept either SQL or a weak type REF CURSOR and return XML.

    Thanks for the help. Actually, I just bought your book and was getting ready to do some research. This should get me pointed in the right direction.
    Does the PL/SQL API lack this functionality? I wasn't sure whether to implement this in PL/SQL or Java, but the decision may be made for me...

  • Using a strongly typed ref cursor doesn't enforce data type

    I am trying to enforce the datatypes of the fields coming back from an oracle reference cursor by making it strongly typed. However, there seems to be no warning at compile time on the oracle side or exception in ODP.NET if the datatype coming back in the cursor does not match. For example here is my cursor and proc
    create or replace
    PACKAGE THIRDPARTY AS
    type pricing_record is record
    BaseIndexRate     number(6,5),
    BaseIndexRateType     VARCHAR2(1 BYTE)
    type cur_pricing2 IS ref CURSOR return pricing_record;
    PROCEDURE getpricingbyappidtest(appid IN application.intid%TYPE, pricing OUT cur_pricing2);
    END THIRDPARTY;
    create or replace
    PACKAGE BODY THIRDPARTY AS
    PROCEDURE getpricingbyappidtest(appid IN application.appid%TYPE, pricing OUT cur_pricing2)
    AS
    BEGIN
         OPEN pricing FOR
         SELECT      somevarcharfield, someothervarcharfield
    FROM application
    WHERE A.appid = appid;
    END getpricingbyappidtest;
    I would expect this wouldn't compile since i am putting a varchar into a number field. But it does. Also if i check the datatype in the reader on the .net side it also is a string. So odp doesn't seem to care what type the cursor is
    here is that code and output
    var schemaTable = reader.GetSchemaTable();
    using (var file = new System.IO.StreamWriter("c:\\_DefinitionMap_" + cursorName + ".txt"))
    file.WriteLine("COLUMN" + "\t" + "DATATYPE");
    foreach (DataRow myField in schemaTable.Rows)
    file.WriteLine(myField["ColumnName"] + "\t" + myField["DataType"]);
    COLUMN     DATATYPE
    BaseIndexRate     System.String
    BaseIndexRateType     System.String
    Does anyone have an approach for enforcing datatypes in a ref cursor? Am I doing something wrong when defining the ref cursor?

    Hello,
    By using a ref cursor you are really using a pointer to a cursor. There is no way I know of to make a compile check of the cursor check unless you want to create a SQL type and cast the cursor to this type and even this wouldn't work in all cases. For instance, I could have function call within my cursor select which could return a varchar (with a number always in the varchar) which would be horribly sloppy but legal and you would expect Oracle to compile it.
    If you are worried about this I would suggest not using ref cursors and go to UDT instead, where there will be more checking (because of a C# equivalence generated object). Oh and BTW, yes the cursor will throw an exception if the data is incorrect, but only at runtime - just like normal Oracle PLSQL.
    Cheers
    Rob.
    http://www.scnet.com.au

  • Help on CAST function, defining TYPE TABLE and using a REF cursor

    Hi,
    I have written a procedure (lookup) inside a package (lookup_pkg) as shown below.
    Procedure has an output variable of type PL/SQL TABLE which is defined in the package.
    I want to write a wrapper procedure lookupref to the procedure lookup to return a ref cursor.
    CREATE OR REPLACE PACKAGE lookup_pkg AS
    TYPE t_lookup_refcur IS REF CURSOR;
    CURSOR c_lookup IS
         Select columns1,2,3,....100
                   FROM A, B, C, D, E
                   WHERE ROWNUM < 1;
    TYPE t_lookup IS TABLE OF c_lookup%ROWTYPE;
    Procedure lookup(id Number, o_lookup OUT t_lookup);
    End lookup_pkg;
    CREATE OR REPLACE PACKAGE BODY lookup_pkg As
    Procedure lookup(id Number, o_lookup OUT t_lookup) IS
    BEGIN
    END lookup;
    Procedure lookupref(id Number, o_lookupref OUT t_lookup_refcur) IS
    o_lookup t_lookup;
    BEGIN
    lookup(id, o_lookup t_lookup);
    OPEN t_lookup_refcur FOR
    SELECT *
         FROM TABLE(CAST(o_lookup AS t_lookup));
    Exception
    End lookupref;
    END lookup_pkg;
    When I compile this procedure, I am getting invalid datatype Oracle error and
    cursor points the datatype t_lookup in the CAST function.
    1. Can anyone tell me what is wrong in this. Can I convert a PL/SQL collection (pl/sql table in this case) to PL/SQL datatype table or does it need to be a SQL datatype only (which is created as a type in database).
    Also, to resolve this error, I have created a SQL type and table type instead of PL/SQL table in the package as shown below.
    create or replace type t_lookuprec as object
                   (Select columns1,2,3,....100
                   FROM A, B, C, D, E
                   WHERE ROWNUM < 1);
    create or replace type t_lookup_tab AS table of t_lookuprec;
    CREATE OR REPLACE PACKAGE BODY lookup_pkg As
    Procedure lookup(id Number, o_lookup OUT t_lookup) IS
    BEGIN
    END lookup;
    Procedure lookupref(id Number, o_lookupref OUT t_lookup_refcur) IS
    o_lookup t_lookup;
    BEGIN
    lookup(id, o_lookup t_lookup);
    OPEN t_lookup_refcur FOR
    SELECT *
         FROM TABLE(CAST(o_lookup AS t_lookup_tab));
    Exception
    End lookupref;
    END lookup_pkg;
    When I compile this package, I am getting "PL/SQL: ORA-22800: invalid user-defined type" Oracle error and
    points the datatype t_lookup_tab in the CAST function.
    2. Can anyone tell me what is wrong. Can I create a type with a select statement and create a table type using type created earlier?
    I have checked the all_types view and found that
    value for Incomplete column for these two types are YES.
    3. What does that mean?
    Any suggestions and help is appreciated.
    Thanks
    Srinivas

    create or replace type t_lookuprec as object
    (Select columns1,2,3,....100
    FROM A, B, C, D, E
    WHERE ROWNUM < 1);You are correct that you need to use CREATE TYPE to use the type in SQL.
    However unless I am mistaken you appear to have invented your own syntax for CREATE TYPE, suggest you refer to Oracle documentation.

Maybe you are looking for

  • ITunes won't accept a payment method... donno what else to say

    Ok, So I'm a US citizen living in Canada, apparently this concept is too much for iTunes to handle. When trying to pay for iTunes it asked for a credit card, I would like to just use free apps, but I guess this is the penalty I pay for going with App

  • Safari won't open after a fresh OS installation

    I went to the Genius Bar because my Mac was freezing on me when booting, so they fix my problem •They installed the Snow leopard (which it was the original it purchased with ) •When I tried to do the migrate all my data using time machine it asked me

  • ORA-39126: Worker unexpected fatal error in KUPW$WORKER

    Any suggestions for error below the comp_name from dba_registry are all VALID OS : Windows 2008 R2 database version 11.2.0.1.0 Import: Release 11.2.0.1.0 - Production on Fri Sep 21 23:15:33 2012 Copyright (c) 1982, 2009, Oracle and/or its affiliates.

  • Translation date from document date instead of posting date

    I have a requirement to set automatically Translation date from Document Date instead of Posting Date as usual in SAP. My company code hasnu2019t got neither 2nd local currency nor 3rd local currency. As usual, when posting transactions in foreign cu

  • CUPS problem

    I dunno each time I have a problem I do not find an appropriate pla to post the message. So going back to the question. My cups does not print anymore, before somebody say "hey idiot did yop plug the cable?" here is the situation I use latest version