Search code examples
c#oracle-databaseodp.netsys-refcursor

ODP .NET Getting a SYSREFCURSOR output. {"Unsupported column datatype"} error


I am trying to execute a procedure that returns a stored procedure. My version of Oracle DB is 9.2 and the ODP .NET version is 10.2.0.100

My C# code looks like this.

OracleCommand od = new OracleCommand();
od.Connection = oc;
OracleParameter opBranchNo;
OracleParameter opSysRef;
od.CommandType = System.Data.CommandType.StoredProcedure;
od.CommandText = "pkg_fetchleaseinfo.proc_fetchleaseheader";

opBranchNo = new OracleParameter("IBRANCH_ID", OracleDbType.Varchar2, 3, "044");
opBranchNo.Direction = System.Data.ParameterDirection.Input;
od.Parameters.Add(opBranchNo);

opSysRef = new OracleParameter();
opSysRef.ParameterName = "REC_SET";
opSysRef.Direction = System.Data.ParameterDirection.Output;
opSysRef.OracleDbType = OracleDbType.RefCursor;
od.Parameters.Add(opSysRef);
od.Prepare();
od.ExecuteNonQuery();
Oracle.DataAccess.Types.OracleRefCursor sysref = 
    (Oracle.DataAccess.Types.OracleRefCursor)opSysRef.Value;
return sysref.GetDataReader();
//OracleDataReader dr1 = 
//((Oracle.DataAccess.Types.OracleRefCursor)opSysRef.Value).GetDataReader();
//return dr1;

My Oracle Procedure code looks like this

PROCEDURE proc_fetchleaseheader(ibranch_id IN VARCHAR2,
    rec_set OUT SYS_REFCURSOR) IS x_rec genericCursor; 
BEGIN
   OPEN x_rec FOR SELECT getleaseheaderrows(ibranch_id) FROM dual;
   rec_set := x_rec;  
EXCEPTION WHEN OTHERS THEN     
   RAISE; 
END;

When I execute my code, the part where I attempt a GetReader() fails with an UNSUPPORTED COLUMN DATATYPE error message.


Solution

  • I believe you are opening a refCursor to hold a Select [RefCursor] from dual

    why don't you just

    PROCEDURE proc_fetchleaseheader(ibranch_id IN VARCHAR2,
        rec_set OUT SYS_REFCURSOR) IS x_rec genericCursor; 
    BEGIN
       x_rec := getleaseheaderrows(ibranch_id);
       rec_set := x_rec;  
    /**EXCEPTION WHEN OTHERS THEN     --no need for this, the proc will raise just fine without being explicitly told to do so
       RAISE; 
    ***/
    END;
    

    or better yet just call getleaseheaderrows from the .net side and drop the procedure (just remember for parameters in ODP it always expects the function return value as the first param.