.net core call oracle function put returned value in first parameter - oracle

.net core 3.1
nuget: Oracle.ManagedDataAccess.Core 2.19.91
Oracle function:
create or replace NONEDITIONABLE FUNCTION GET_TOTAL_SALES
(
PARAM1 IN VARCHAR2,
PARAM2 IN VARCHAR2
)
RETURN VARCHAR2 AS
BEGIN
RETURN 'abc';
END GET_TOTAL_SALES;
.net core code:
var cmdText = "GET_TOTAL_SALES";
using (var connection = new OracleConnection(connStr))
{
using (var command = new OracleCommand(cmdText, connection))
{
connection.Open();
command.CommandType = CommandType.StoredProcedure;
var param1 = new OracleParameter("PARAM1", OracleDbType.Varchar2);
param1.Direction = ParameterDirection.Input;
param1.Value = "param1Value";
command.Parameters.Add(param1);
var param2 = new OracleParameter("PARAM2", OracleDbType.Varchar2);
param2.Direction = ParameterDirection.Input;
param2.Value = "param2Value";
command.Parameters.Add(param2);
var returnValue = new OracleParameter("returnValue", OracleDbType.Varchar2);
returnValue.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnValue);
command.ExecuteNonQuery();
Console.WriteLine(returnValue.Value); //""
Console.WriteLine(param1.Value); //"abc"
}
}
The result is that the returned value is putted into the first parameter (param1) and not into the returnValue. Why? Help!

I asked Oracle and got a reply:
"This is expected behavior. The first parameter bound for a function must be the return value. From the doc:
https://docs.oracle.com/en/database/oracle/oracle-data-access-components/19.3.2/odpnt/featOraCommand.html
I know, it is crazy.
We can also use binding by name:
command.BindByName = true;
and then order doesn't matter.

Related

Calling Oracle procedure from entity framework with boolean output parameter [duplicate]

Is it possible to correctly pass an OracleParameter to a boolean parameter in a pl/sql stored procedure?
I used the following workaround to bypass this limitation:
Wrap the function call using an anonymous block.
Return an output variable containing 1 or 0.
Read the output variable and cast it to boolean.
Here is some sample code:
using (var connection = new OracleConnection("<connection string>"))
{
var command = new OracleCommand();
command.Connection = connection;
command.CommandText =
"declare v_bool boolean;" +
"begin " +
"v_bool := auth_com.is_valid_username (:username); "+
"if (v_bool = TRUE) then select 1 into :v_result from dual; end if; " +
"if (v_bool = FALSE) then select 0 into :v_result from dual; end if; " +
"end;";
command.Parameters.Add(new OracleParameter { ParameterName = "username", OracleDbType = OracleDbType.NVarchar2, Size=512, Direction = ParameterDirection.Input });
command.Parameters.Add(new OracleParameter { ParameterName = "v_result", OracleDbType = OracleDbType.Decimal, Direction = ParameterDirection.Output });
try
{
connection.Open();
command.ExecuteNonQuery();
}
finally
{
connection.Close();
}
bool success = Convert.ToBoolean(((OracleDecimal)command.Parameters["v_result"].Value).ToInt32());
}
EDIT:
Alex Keh from Oracle, october 2013:
We're planning on supporting ODP.NET Boolean in the managed provider
in the near term, possibly in the middle of next year.
You can not use boolean parameters in SQL. So calling an stored procedure that takes or returns a boolean value won't work in SQL. There is no problem using such a procedure from within a pl/sql block.
ADDED from JCallico answer:
I used the following workaround to bypass this limitation:
Wrap the function call using an anonymous block.
Return an output variable containing 1 or 0.
Read the output variable and cast it to boolean.
Here is some sample code:
using (var connection = new OracleConnection("<connection string>"))
{
var command = new OracleCommand();
command.Connection = connection;
command.CommandText =
"declare v_bool boolean;" +
"begin " +
"v_bool := auth_com.is_valid_username (:username); "+
"if (v_bool = TRUE) then select 1 into :v_result from dual; end if; " +
"if (v_bool = FALSE) then select 0 into :v_result from dual; end if; " +
"end;";
command.Parameters.Add(new OracleParameter { ParameterName = "username", OracleDbType = OracleDbType.NVarchar2, Size=512, Direction = ParameterDirection.Input });
command.Parameters.Add(new OracleParameter { ParameterName = "v_result", OracleDbType = OracleDbType.Decimal, Direction = ParameterDirection.Output });
try
{
connection.Open();
command.ExecuteNonQuery();
}
finally
{
connection.Close();
}
bool success = Convert.ToBoolean(((OracleDecimal)command.Parameters["v_result"].Value).ToInt32());
}
EDIT:
Alex Keh from Oracle, october 2013:
We're planning on supporting ODP.NET Boolean in the managed provider
in the near term, possibly in the middle of next year.

Cannot retrieve out parameters from oracle stored procedure using OracleManagedDataAccess

I m writing my issue here after lot of struggle and trying all the available options online.
Here is my issue. I am using ODP.Net, oracleManagedDataAccess library to connect to the oracle database.I have a stored procedure with in and out parameters which works fine when I test it from pl/sql but when I am trying to execute it from my .net code and retrieve the out parameters they return null and if I see the status of each out parameter, its false and value is "Null Fetched" and also the size of the parameter is showing up as 0, though I set the size of the parameter to 4000 for a string type. Please see my code below. Please help. As I told you earlier, my stored proc works just fine from pl/sql.
connection = new OracleConnection(DBHelper.ConnectionString);
connection.Open();
command = new OracleCommand();
command.Connection = connection;
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = DBConstants.PROC_GETBORROWERSEQNO;
//Input
command.Parameters.Add("password", OracleDbType.Varchar2, 4000, password, ParameterDirection.Input);
command.Parameters.Add("userid", OracleDbType.Varchar2, 4000, userID, ParameterDirection.Input);
command.Parameters.Add("ipaddress", OracleDbType.Varchar2,4000, iPAddress, ParameterDirection.Input);
//Output
command.Parameters.Add("shawcustno", OracleDbType.Decimal).Direction = ParameterDirection.Output;
command.Parameters.Add("emailid", OracleDbType.Varchar2,4000).Direction = ParameterDirection.Output;
command.Parameters.Add("contr_phase", OracleDbType.Varchar2, 4000).Direction = ParameterDirection.Output;
command.Parameters.Add("source_seqno", OracleDbType.Decimal,15).Direction = ParameterDirection.Output;
command.Parameters.Add("borrower_seqno", OracleDbType.Decimal,15).Direction = ParameterDirection.Output;
command.Parameters.Add("pag_phone", OracleDbType.Varchar2, 4000).Direction = ParameterDirection.Output;
command.Parameters.Add("pag", OracleDbType.Varchar2, 4000).Direction = ParameterDirection.Output;
//InputOutput
command.Parameters.Add("sessionseqno", OracleDbType.Decimal).Direction = ParameterDirection.InputOutput;
sequenceNumber = command.ExecuteNonQuery();
customer = new Customer();
customer.Company = command.Parameters["company"].Value.ToString();
customer.Pag = command.Parameters["pag"].GetString();
customer.PagPhone = command.Parameters["pag_phone"].GetString();
customer.BorrowerSeqNo = command.Parameters["borrower_seqno"].IsDBNull() ? 0 : command.Parameters["borrower_seqno"].GetInt32();
customer.SourceSeqNo = command.Parameters["source_seqno"].IsDBNull() ? 0 : command.Parameters["source_seqno"].GetInt32();
customer.EmailID = command.Parameters["emailid"].GetString();
customer.ShawCustNo = command.Parameters["shawcustno"].GetString();
customer.SessionSeqNo = command.Parameters["sessionseqno"].IsDBNull() ? 0 : command.Parameters["sessionseqno"].GetInt32();
}
finally found the answer, the order of the arguments has to be exactly the same as that declared in the stored procedure. I missed that order.

DbContext stored procedures oracle 11g

I am attempting to execute a stored procedure that has several in/out parameters. This is what the expected way of doing this with SQLServer would be.
using (Context)
{
const string xml = "<test><testc>testing</testc></test>";
var userIdParam = new SqlParameter { ParameterName = "I_USER_ID", Value = user.ID };
var dtsParam = new SqlParameter { ParameterName = "I_DTS", Value = DateTime.Now };
var timeoutParam = new SqlParameter { ParameterName = "I_TIMEOUT", Value = DateTime.Now };
var xmlParam = new SqlParameter { ParameterName = "I_XML", Value = xml };
var sessionIdParam = new SqlParameter { ParameterName = "O_SESSION_ID", Value = "", Direction = ParameterDirection.Output };
var result =
Context.Database.SqlQuery<string>(
"DALLEN.SP_INSERT_SESSION I_USER_ID, I_DTS, I_TIMEOUT, I_XML, O_SESSION_ID out",
userIdParam,
dtsParam,
timeoutParam,
xmlParam,
sessionIdParam);
}
Unfortunately this does not work with oracle. I understand that ref cursors might be used in place of out parameters but that is not my own issue. Is there a good way of passing parameters to the stored procedure using dbcontext.database.sqlquery?
EDIT
Error message received when the query attempts to execute. It explains that it doesn't accept sqlParamters so I changed them to OracleParamters and that didn't work either.
"Unable to cast object of type 'System.Data.SqlClient.SqlParameter' to type 'Oracle.DataAccess.Client.OracleParameter'."
Procedure:
CREATE OR REPLACE PROCEDURE DALLEN.SP_INSERT_SESSION (
I_USER_ID IN NUMBER,
I_DTS IN DATE,
I_TIMEOUT IN DATE,
I_XML IN VARCHAR2,
O_SESSION_ID OUT NUMBER
)
AS
BEGIN
SELECT S_SESSION.NEXTVAL
INTO O_SESSION_ID
FROM DUAL;
INSERT INTO DALLEN.SYS_SESSION
(ID, USER_ID, DTS, TIMEOUT, XML)
VALUES (O_SESSION_ID, I_USER_ID, I_DTS, I_TIMEOUT, I_XML);
END;
Thank you in advance!

Multiple result set from SQL Via LiNQ

I have a stored procedure as
pr___GetArchiveData
Select * from TABLE1
SELECT * FROM TABLE2
SELECT * FROM TABLE 3
I want to get this result set into a dataset. Or access the values of three select queries!!
I have a DBML file in which when i drag and drop the stored procedure generates a code as follows:-
global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.pr___GetArchiveData")]
public ISingleResult<pr___GetArchiveDataResult> pr___GetArchiveData([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="UniqueIdentifier")] System.Nullable<System.Guid> projectID)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), projectID);
return ((ISingleResult<pr__Project_pr___GetArchiveData>)(result.ReturnValue));
}
In the code MVC3 Architecture + LINQ i have written a code to get the result set as follows :-
using (HBDataContext hb = new HBDataContext())
{
System.Data.DataSet ds = new System.Data.DataSet();
String connString = connString;
var conn = new System.Data.SqlClient.SqlConnection(connString);
var cmd = conn.CreateCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "pr__GetArchiveData";
cmd.Connection.Open();
var mReader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);
//var reader = cmd.ExecuteReader();
//using (System.Data.SqlClient.SqlDataReader mReader = cmd.ExecuteReader())
//{
// while (mReader.Read())
//{
// mReader.Read();
var tbl1 = hb.Translate<tbl1 >(mReader).ToList();
// mReader = cmd.ExecuteReader();
mReader.NextResult();
var tbl2 = hb.Translate<tbl2 >(mReader).ToList();
mReader.NextResult();
var tbl3 = hb.Translate<tbl3>(mReader).ToList();
// }
// }
}
But while running it throws error as -
"Invalid attempt to call NextResult when reader is closed."
I am not sure where i am wrong!!
I have tried using it as while
(mReader.Read())
Kindly suggest!!!!
The code gen for ISingleResult won't provide multiple result sets
Try adding your own IMultipleResults wrapper - see the tutorial in http://blogs.msdn.com/b/dditweb/archive/2008/05/06/linq-to-sql-and-multiple-result-sets-in-stored-procedures.aspx
In .dbml file for the stored procedure the code will be like
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.pr__Home_GetArchiveData")]
public ISingleResult<pr__Home_GetArchiveData> pr__Home_GetArchiveData([global::System.Data.Linq.Mapping.ParameterAttribute(Name="AlbumID", DbType="UniqueIdentifier")] System.Nullable<System.Guid> albumID)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), albumID);
return ((ISingleResult<pr__Album_GetAlbumsFilesResult>)(result.ReturnValue));
}
Replace it with IMultipleResult as below `
[global::System.Data.Linq.Mapping.FunctionAttribute(Name = "dbo.pr__Home_GetArchiveData")]
[ResultType(typeof(tbl1))]
[ResultType(typeof(tbl2))]
[ResultType(typeof(tbl3))]
[ResultType(typeof(tbl4))]
public IMultipleResults pr__Home_GetArchiveData([global::System.Data.Linq.Mapping.ParameterAttribute(Name = "HOMEID", DbType = "UniqueIdentifier")] System.Nullable hOMEID)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), hOMEID);
return ((IMultipleResults)result.ReturnValue);
}
in the code .
using (HBDataContext hb = new HBDataContext())
{
using (System.Data.Linq.IMultipleResults _results = hb.pr__Home_GetArchiveData(model.HomeID))
{
List<tbl1> _tbl1= _results.GetResult<tbl1>().ToList();
List<tbl2> _tbl2= _results.GetResult<tbl2>().ToList();
List<tbl3> _tbl3= _results.GetResult<tbl3>().ToList();
List<tbl4> _tbl4= _results.GetResult<tbl4>().ToList();}}
You will get the values of the Select queries from theStoredProcedure ...

How do I call an Oracle function to insert data using ODP.NET and a dataset? (specific example)

We're really lost on this one, having read the ODP.NET 2 Day+ developer guide hasn't helped. I've provided the function definition (stored in a package), I don't understand what we have to cast the dataset to or what to pass the function. Here is the function definition:
FUNCTION ins (
rec_data IN OUT schema.table%ROWTYPE,
p_rowid OUT ROWID,
p_execution_ts IN schema.table.update_ts%TYPE)
RETURN NUMBER
Here is what we have done (which does nothing):
// inserts data
public void insertData(DataSet Data)
{
string connStr = "DATA SOURCE=someValidConnString";
OracleConnection conn = new OracleConnection(connStr);
string rowID = String.Empty;
Int32 rtnVal = 0;
try
{
conn.Open();
OracleCommand insCmd = new OracleCommand("PACKAGE.ins", conn);
insCmd.CommandType = CommandType.StoredProcedure;
OracleParameter outParam2 = new OracleParameter("retVal", OracleDbType.Varchar2, rtnVal,
ParameterDirection.ReturnValue);
insCmd.Parameters.Add(outParam2); //return value
OracleParameter inParam1 = new OracleParameter("rec_data", OracleDbType.NVarchar2, dsACCTData.Tables
[0].Rows[0], ParameterDirection.InputOutput);
OracleParameter outParam = new OracleParameter("p_rowid", OracleDbType.Varchar2, rowID,
ParameterDirection.Output);
OracleParameter inParam2 = new OracleParameter("p_execution_ts", OracleDbType.Date,
Oracle.DataAccess.Types.OracleDate.GetSysDate(), ParameterDirection.Input);
insCmd.Parameters.Add(inParam1); //first in out parameter
insCmd.Parameters.Add(outParam); //second out parameter
insCmd.Parameters.Add(inParam2); //third in parameter
insCmd.ExecuteNonQuery();
conn.Close();
}
catch (OracleException ee)
{
throw ee;
}
finally
{
conn.Dispose();
}
}
I know this is a vry specific question but I'm really lost. Let's assume the Oracle function ins works (it does), in this instance we simply don't know how to call it correctly using ODP.NET
Many thanks for any help.
edit: here is the error message:
ORA-06550: line 1, column 15:
PLS-00306: wrong number or types of arguments in call to 'INS'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
Kind regards,
Fugu
maybe this could help:
http://www.c-sharpcorner.com/UploadFile/john_charles/CallingOraclestoredproceduresfromMicrosoftdotNET06222007142805PM/CallingOraclestoredproceduresfromMicrosoftdotNET.aspx
I'm looking for the same answer and I think I found something. There is example of the output variable in second row.
objCmd.Parameters.Add("pin_deptno", OracleType.Number).Value = 20;
objCmd.Parameters.Add("pout_count", OracleType.Number).Direction = ParameterDirection.Output;
Hope that link will do any good.
Regards, M
Every thing looks good. But you have to check all attribute name and attribute types.
In your example have bad data type OracleDbType.Varchar2, because your function return Number (in your Oracle Function definition)
OracleParameter outParam2 = new OracleParameter("retVal", OracleDbType.Varchar2, rtnVal,
ParameterDirection.ReturnValue);
insCmd.Parameters.Add(outParam2); //return value

Resources