Error: Parameter Type is not supported in ADODB code of classic ASP - oracle

I have written code to insert call parameterized stored procedure written in oracle pl/sql. I have given all parameters properly as displayed in below code.
function CallSp(str_id, ref_no, note, userId, strdatestamp, writtenDate)
Dim strcon2 : set strcon2=server.createObject("ADODB.Connection")
Dim strcmd2
Dim sql2
Dim ReturnVal
strcon2.Open "Proper Connectionstring provided here"
sql2 = "Fr_Store_Notes"
Set strcmd2 = Server.CreateObject("ADODB.Command")
Set strcmd2.ActiveConnection = strCOn2
strcmd2.CommandText = sql2
strcmd2.CommandType = 4
strcmd2.Parameters.Refresh
strcmd2.Parameters.Append strcmd2.CreateParameter("p_str_id", 12,1)
strcmd2.Parameters("p_str_id") = str_id
strcmd2.Parameters.Append strcmd2.CreateParameter("p_ref_no", 12,1)
strcmd2.Parameters("p_ref_no") = ref_no
strcmd2.Parameters.Append strcmd2.CreateParameter("p_UserId", 12,1)
strcmd2.Parameters("p_UserId") = userId
strcmd2.Parameters.Append strcmd2.CreateParameter("p_note", 12,1)
strcmd2.Parameters("p_note") = note
strcmd2.Parameters.Append strcmd2.CreateParameter("p_Datestamp", 12,1)
strcmd2.Parameters("p_Datestamp") = strdatestamp
strcmd2.Parameters.Append strcmd2.CreateParameter("p_WrittenDate", 12,1)
strcmd2.Parameters("p_WrittenDate") = writtenDate
strcmd2.Parameters.Append strCmd2.CreateParameter("p_return", 3, 2)
strcmd2.Execute
ReturnVal = strcmd2.Parameters("p_return").Value
CallSp=ReturnVal
set strCmd2=Nothing
strCon2.close
end function
But I am receiving error as
Parameter Type is not supported at the line strcmd2.Execute
Database stored procedure is as like below and working fine if we execute it from database
create or replace
procedure Fr_Store_Notes (
P_STR_ID IN VARCHAR2,
p_Ref_no in VARCHAR2,
P_UserId in VARCHAR2,
P_Note IN VARCHAR2,
P_datestamp IN VARCHAR2,
p_WrittenDate IN VARCHAR2,
p_return OUT number)
AS
BEGIN
--Expected Code Block is there and working fine
End;
Can anyone help me in sorting out this issue

Update: - Apparently after a bit of research (as I don't work with Oracle) ADODB doesn't support adVariant (which is 12) and you should use adVarChar (which is 200) instead.
See A: Classic ASP calling Oracle stored procedure with OraOleadb Driver
Leaving the rest of the answer below as it's probably still relevant once this issue is fixed.
The cause is of that particular error is usually a mismatch of data type once the ADODB talks to the provider defined by the connection.
Just looking at the procedure definition in Oracle in comparison to your ADODB.Command object I can see that the p_return parameter appears to be incorrect. I talk about this in a previous answer to a similar question.
According to Data Type Mapping (a great resource for Data Type Mapping in ADO) adInteger (which is 3) maps to Int in Oracle not Number. Instead, you should use adNumeric (which is 131) which should fix that particular error.
Try changing this line
strcmd2.Parameters.Append strCmd2.CreateParameter("p_return", 3, 2)
to
strcmd2.Parameters.Append strCmd2.CreateParameter("p_return", 131, 2)
Useful Links
A: Using Stored Procedure in Classical ASP .. execute and get results
A: ADODB.Parameters error '800a0e7c' Parameter object is improperly defined. Inconsistent or incomplete information was provided (recommend this to learn how to use METADATA in global.asa to have ADO Named Constants always available to an ASP Web Application)

Related

Calling an Oracle stored procedure in Dapper

I have an application that was calling an Oracle stored procedure by passing a string with the parameters in the string, i.e.
string Query = "CALL MySP(" + Variable1 + ", " + Variable2 + ")";
This worked, or at least the last time I tried it it worked.
I need to rewrite the query using DynamicParameters.
However when I do an Execute(Query, Parms, commandType: CommandType.StoredProcedure), I get errors.
Here is an example of what I tried:
using(OracleConnection conn = new OracleConnection(myConnectionString)
{
string Query = "CALL MySP(:Var1, :Var2)";
DynamicParameters parms = new DynamicParameters();
parms.Add("Var1", Variable1, DbType.String, ParameterDirection.Input);
parms.Add("Var2", Variable2, DbType.String, ParameterDirection.Input);
var results = connection.Execute(Query, parms, commandType: CommandType.StoredProcedure);
}
When I run it with the above query, I get this error:
ORA-06550: line 1, column 12:
PLS-00103: Encountered the symbol "MySP" when expecting one of the following:
:= . ( # % ;
The symbol ":=" was substituted for "MySP" to continue.
If I remove the CALL statement:
Query = "MySP(:Var1, :Var2);
then I get this error message:
ORA-01008: not all variables bound
Can anyone see what I am doing wrong?
The problem is that I keep looking on the C# and Dapper side of the equation. Our Oracle DBA set the permissions on the Stored Procedure and it ran.
However I could not tell Dapper that the CommandType was Stored Procedure and my string to call the stored procedure had to look like:
string Query = "CALL MySP(:Var1, :Var2)";
If I removed "CALL" I got an error. If I added "commandType: CommandType.StoredProcedure" to the Dapper Execute function I got a binding error.
Our DBA said he was going to look more into it to see what he can find out on his end so that we can call the stored procedure using the CommandType.StoreProcedure parameter in the Execute function.

Calling stored proc that has default value parameters

I have a stored procedure with signature
PROCEDURE store_cust_response(
p_id NUMBER DEFAULT NULL,
p_camp_id NUMBER DEFAULT NULL,
p_offer_id NUMBER DEFAULT NULL
)
When creating parameters in VBSCript, do I have to create one for each parameter in the signature? If so, how do I then invoke their default values?
Set conncmdA = CreateObject("adodb.command")
conncmdA.CommandText = "foo.store_cust_response"
conncmdA.CommandType = 4 'adCmdStoredProc
conncmdA.ActiveConnection = conntemp
conncmdA.Parameters.Append conncmdA.CreateParameter("p_id", adInteger, adParamInput, 4)
conncmdA.Parameters.Append conncmdA.CreateParameter("p_camp_id", adInteger, adParamInput, 4)
conncmdA.Parameters.Append conncmdA.CreateParameter("p_offer_id", adInteger, adParamInput, 4)
conncmdA.Parameters(0) = null
conncmdA.Parameters(1) = camp_id
conncmdA.Parameters(2) = offer_id
conncmdA.Execute
Set conncmdA = Nothing
IIRC default values are only applied if the procedure is invoked from PL/SQL. (My understanding is that the PL/SQL compiler grabs the default values from the data dictionary if the particular invocation of the procedure in PL/SQL doesn't supply all the parameters). If you're invoking the procedure some other way you have to supply all parameters. As far as "invoke their default values" - you can't. You have to supply values, even if those values are the same as the defaults in the signature. I suppose you could query the data dictionary to obtain the default values - something like
SELECT a.DEFAULT_VALUE
FROM SYS.DBA_ARGUMENTS a
WHERE a.OBJECT_NAME = whatever AND
a.ARGUMENT_NAME = whatever;
One problem with this is that DBA_ARGUMENTS.DEFAULT_VALUE is a LONG (similar to a BLOB but more annoying to deal with) which you'd have to fetch and interpret.
Best of luck.

How to call an Oracle function with a Ref Cursor as Out-parameter from C#?

I'm using a product that provides a database API based on Oracle functions and I'm able to call functions via ODP.NET in general. However, I can't figure out, how to call a function that includes a Ref Cursor as Out-parameter. All the samples I found so far either call a procedure with Out-parameter or a function with the Ref Cursor as return value. I tried to define the parameters similiarly, but keep getting the error that the wrong number or type of parameters is supplied.
Here is the function header (obviously obfuscated):
FUNCTION GetXYZ(
uniqueId IN somepackage.Number_Type,
resultItems OUT somepackage.Ref_Type)
RETURN somepackage.Error_Type;
These are the type definitions in "somepackage":
SUBTYPE Number_Type IS NUMBER(13);
TYPE Ref_Type IS REF CURSOR;
SUBTYPE Error_Type IS NUMBER;
And this is the code that I have tried:
string sql = "otherpackage.GetXYZ";
var getXYZCmd = OracleCommand oracleConnection.CreateCommand(sql);
getXYZCmd.CommandType = CommandType.StoredProcedure;
getXYZCmd.Parameters.Add("uniqueId", OracleDbType.Int32).Value = uniqueExplosionId;
getXYZCmd.Parameters.Add("resultItems", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
getXYZCmd.Parameters.Add("return_value", OracleDbType.Int32).Direction = ParameterDirection.ReturnValue;
The I tried the following different ways to call the function (of course only one at a time):
var result = getXYZCmd.ExecuteNonQuery();
var reader = getXYZCmd.ExecuteReader();
var scalarResult = getXYZCmd.ExecuteScalar();
But each of them fails with the error message:
Oracle.DataAccess.Client.OracleException: ORA-06550: line 1, column 15:
PLS-00306: wrong number or types of arguments in call to 'GETXYZ'
ORA-06550: line 1, column 15:
PLS-00306: wrong number or types of arguments in call to 'GETXYZ'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored.
So is it generally possible to call a function with a Ref Cursor as Out-parameter from C# with ODP.NET? I can call a function with the same structure with a Varchar2-Out-parameter instead of the Ref Cursor without problems...
Btw, I'm using ODP.NET version 2.112.2.0 from C#.NET 3.5 in Visual Studio 2008.
Thanks in advance for your help!
You sure can. There are a few gotchas to be wary of but here is a test case
create or replace function testodpRefCursor(
uniqueId IN NUMBER
,resultItems OUT NOCOPY SYS_REFCURSOR) RETURN NUMBER
IS
BEGIN
OPEN resultItems for select level from dual connect by level < uniqueId ;
return 1;
END testodpRefCursor;
I have found that
functions likes to have the
ReturnValue as THE FIRST param
in the collection
BindByName is by default FALSE, so it defaults to BIND BY POSITION
Otherwise it is quite straight forward:
OracleCommand cmd = new OracleCommand("TESTODPREFCURSOR", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.BindByName = true;
// Bind
OracleParameter oparam = cmd.Parameters.Add("ReturnValue", OracleDbType.Int64);
oparam.Direction = ParameterDirection.ReturnValue ;
OracleParameter oparam0 = cmd.Parameters.Add("uniqueId", OracleDbType.Int64);
oparam0.Value = 5 ;
oparam0.Direction = ParameterDirection.Input;
OracleParameter oparam1 = cmd.Parameters.Add("resultItems", OracleDbType.RefCursor);
oparam1.Direction = ParameterDirection.Output;
// Execute command
OracleDataReader reader;
try
{
reader = cmd.ExecuteReader();
while(reader.Read() ){
Console.WriteLine("level: {0}", reader.GetDecimal(0));
}
} ...
Now for more samples go to your Oracle Home directory and look # the Ref cursor samples in ODP.NET
for instance:
%oracle client home%\odp.net\samples\4\RefCursor
hth

what are the OleDbTypes associated with Oracle Number and varchar2 when calling a function

I'm trying to map OleDb parameters to an Oracle Function. I was able to do this using the System.Data.Oracle namespace but then found that this is depricated, so I thought i would re-write it as OldDb to avoid installing the Oracle Provider.
I have defined the following oracle function as an example:
create function GetImagePath (AIRSNumber in number)
return varchar2
is
begin
return '\\aiimg524\images\Ofndrtrk\2010\01\0kvrv1p000lcs74j';
end;
and I'm calling it using the following code:
using (var command = new OleDbCommand())
{
command.Connection = con;
command.CommandText = ConfigurationManager.AppSettings[OTRAK_PHOTO_FUNC];
command.CommandType = CommandType.StoredProcedure;
string parm = ConfigurationManager.AppSettings[OTRAK_PHOTO_PARM];
command.Parameters.Add(parm, OleDbType.Decimal); // maps to oracle Number
command.Parameters[parm].Direction = ParameterDirection.Input;
command.Parameters[parm].Value = airsNumber;
command.Parameters.Add(RETURN_VALUE, OleDbType.Variant); // maps to Oracle varchar2
command.Parameters[RETURN_VALUE].Direction = ParameterDirection.ReturnValue;
try
{
con.Open();
command.ExecuteNonQuery();
path = command.Parameters[RETURN_VALUE].Value.ToString();
}
I tried a bunch of different OleDB types for the parameter and the return value. the current attempt is from a mapping table i found on the web that said number = decimal and varchar2 = variant. I'm about to try every permutation of types in the enum and wanted to ask for help. the not so useful error message i get is:
[System.Data.OleDb.OleDbException] = {"ORA-06550: line 1, column 7:\nPLS-00306: wrong number or types of arguments in call to 'GETIMAGEPATH'\nORA-06550: line 1, column 7:\nPL/SQL: Statement ignored"}
This actually had nothing to do with the type of the parameters but the order. Using the OleDb provider for Oracle does not respect the names of the parameters in the parameter collection but rather the order that the parameters are added. Wwhen calling an oracle function, the return value is a free parameter that must be declared first. by adding my return value parameter and then the actual function parameter things started working.
using the command.Parameters.AddWithValue(parm, value) also simplifies things a bit.

Oracle Stored Procedure with out parameter using Nhibernate

How can I access the value of an out parameter of an oracle stored procedure in the .net code - Oracle stored procedure being called via Nhibernate?
Sample working code would help.
You have to use the latest version of NHibernate (2.1.2).
<sql-query name="ReturnSomethig" callable="true">
<return class="Somethig" />
{ call ReturnSomethig(:someParameter) }
</sql-query>
The Oracle Stored Procedure need to has the first parameter as a out sys_refcursor parameter.
And you can call the named query like that:
IQuery query = currentSession.GetNamedQuery("ReturnSomethig");
query.SetInt64("someParameter", someParameter);
var somethig = query.List<Somethig>();
And it will work.
I tried the 2.1.2 libraries without much luck. I had to actually make some modifications to the library based on this article. If you go this route, you'll want to make sure you are using the Oracle.DataAccess dll since it won't work with System.DataAccess.OracleClient dll.
Whilst looking into getting out parameters from NHibernate I found various links suggesting that to get a stored proc out parameter value (rather than using a refcursor) you need to construct the command in code rather than using the XML mappings file.
But I found difficulty finding a complete working example online. It's not a beautiful solution, but I post in case it helps someone.
Example Oracle stored proc:
create or replace PACKAGE BODY MY_PACKAGE AS
PROCEDURE add_one
(
p_out out number,
p_in in number
) AS
BEGIN
select p_in + 1 into p_out from dual;
END add_one;
END MY_PACKAGE;
Example C# code, where sessionFactory is an NHibernate ISessionFactory:
using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
var command = session.Connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "MY_PACKAGE.add_one";
var parmOut = command.CreateParameter();
parmOut.ParameterName = "p_out";
parmOut.DbType = DbType.Decimal;
parmOut.Direction = ParameterDirection.Output;
command.Parameters.Add(parmOut);
var parmIn = command.CreateParameter();
parmIn.ParameterName = "p_in";
parmIn.DbType = DbType.Decimal;
parmIn.Direction = ParameterDirection.Input;
parmIn.Value = 67;
command.Parameters.Add(parmIn);
transaction.Enlist(command);
command.ExecuteNonQuery();
int sixtyEight = (int) (decimal) parmOut.Value;
}
See this comment by Richard Brown. Some sample code can be found here.
Unfortunately I can't test it so I don't know whether it works or not.
A belated reply.
For oracle, out parameter is not supported by nHibernate unless it is a cursor. If you just want a scalar value, a work around is to wrap your stored procedure with an oracle function.
Then you can do this
<sql-query name="TestOracleFunction" callable="true">
<return-scalar column="MyOutputValue" type="String" />
<![CDATA[
select MyOracleFunction as MyOutputValue from dual
]]>
</sql-query>
This works!

Resources