Calling stored proc that has default value parameters - oracle

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.

Related

Oracle: Update from within procedure not working

In my Oracle PL/SQL procedure I am trying to update a row like this:
UPDATE personal p
SET p.surname = surname, p.name = name, p."alter" = alter, p.sex = sex, p.jobcode = jobcode, p.year_wage = month_wage * 12
WHERE p.personalnr = personalnr;
COMMIT;
I have added these two statements right after the commit to confirm the code is reached and executed with the right arguments (e.g. here I want to change the name):
DBMS_OUTPUT.put_line('updated ' || name);
DBMS_OUTPUT.put_line('personalnr ' || personalnr);
Now this update-statement is part of a procedure that is called from within another procedure.
However, the changes are not applied and the name will remain the same even tho the update was executed. I have tried to use an exception-handler as well and there doesn't seem to be any exception happening. I can confirm that the WHERE-clause is as intendet. There is one record that matches the predicate.
Now the strange thing:
When I change the code to the example below, an update happens. However it updates every record and not only the one with the right personalnr. Again: the routine is called only once with one personalnr that matches only one entry in the table.
UPDATE personal p
SET p.name = 'test'
WHERE p.personalnr = personalnr;
COMMIT;
It is working, but it's updating all rows in the table (or at least, those where personalnr is not null), not just the one you expect.
From the documentation:
If a SQL statement references a name that belongs to both a column and either a local variable or formal parameter, then the column name takes precedence.
You have a PL/SQL variable that has the same name as a column. When you do
where p.personalnr = personalnr
you are really doing:
where p.personalnr = p.personalnr
and the same thing happens in the set part; SET p.surname = surname updates the column value to whatever value it had before, not the PL/SQL variable's value. So it looks like the update didn't happen- it actually did, but because everything was set to the same as it's original value it doesn't look like anything happened. (Except - all rows will now have the same year_wage value...)
You can either prefix your variables with the procedure name:
where p.personalnr = my_proc.personalnr
or change the variable names so they don't conflict; it's common to use a short prefix, e.g. l_ for a local variable, or p_ for a passed-in parameter, etc.
where p.personalnr = l_personalnr
Remember to do that for the set part too, or your update still won't appear to do anything.
UPDATE personal p
SET p.surname = l_surname, p.name = l_name, p."alter" = l_alter,
p.sex = l_sex, p.jobcode = l_jobcode, p.year_wage = l_month_wage * 12
WHERE p.personalnr = l_personalnr;
You need to change the parameter name something other than the table's column name.
UPDATE personal p
SET p.name = 'test'
WHERE p.personalnr = personally;
-- here condition is column_name = column_name
-- This will be true for all the records of the table
Change personalnr --> p_personalnr and it will work for you

Oracle Entity Framework Core pass table parameter to stored procedure

I am trying to pass a parameter to a stored procedure using the Oracle.EntityFrameworkCore package like this:
DataTable table = new DataTable();
table.Columns.Add("keyColumn", typeof(string));
table.Columns.Add("valueColumn", typeof(string));
var row = table.NewRow();
row.ItemArray = new object[]
{
entry.KeyColumn,
entry.ValueColumn
};
table.Rows.Add(row);
var parameter = new OracleParameter("entries",table);
parameter.UdtTypeName = "entry_type_list";
return context.Database.ExecuteSqlCommandAsync(
new RawSqlString( #"EXEC set_entry_list (:entries)" ),
parameter);
The stored procedure and type are defined like this:
CREATE OR REPLACE TYPE entry_type AS OBJECT
(
"keyColumn" NVARCHAR2(3),
"valueColumn" NVARCHAR2(3)
);
CREATE OR REPLACE TYPE entry_type_list AS TABLE OF entry_type;
CREATE OR REPLACE PROCEDURE set_entry_list (entries entry_type_list) AS
BEGIN
REM Doing stuff
END;
But I get an error:
System.ArgumentException: Value does not fall within the expected range.
at Oracle.ManagedDataAccess.Client.OracleParameter..ctor(String parameterName, Object obj)
The only sources for this is an answer how to do this with SQL Server, but no answer for Oracle with EFCore. The issue here seems to be that Oracle only accepts an OracleParameter whereas others use SqlParameter.
If I use the SqlParameter type like this:
var parameter = new SqlParameter("entries", SqlDbType.Structured);
parameter.TypeName = "entry_type_list";
parameter.Value = table;
I get this error:
System.InvalidCastException: Unable to cast object of type'System.Data.SqlClient.SqlParameter' to type 'Oracle.ManagedDataAccess.Client.OracleParameter'.
I also did try setting parameter.OracleDbType to different values like Blob, RefCursor, Clob or XmlType, setting parameter.DbType to Object or setting CollectionType to PLSQLAssociativeArray with no success. Also passing a list or an array of objects instead of a table did not succeed.
I currently have no idea what else I could try.
Any method to pass a big amount of entities to a stored procedure in a performant way would help. I use them with the merge-command so I need to be able to convert those parameters to a table.
I now found a solution using a temporary table and using this one as my input parameter.
As I can't pass a complete table, but an array of simple objects I have to fill this table by passing one array for each column:
var keyColumn = new OracleParameter( "keyColumn", OracleDbType.Decimal );
keyColumn.Value = values.Select( c => c.KeyColumn).ToArray();
var valueColumn = new OracleParameter( "valueColumn", OracleDbType.Decimal );
valueColumn = values.Select( c => c.ValueColumn).ToArray();
using ( var transaction = this.dbContext.Database.BeginTransaction( IsolationLevel.ReadCommitted) )
{
var connection = this.dbContext.Database.GetDbConnection() as OracleConnection;
OracleCommand cmd = connection.CreateCommand();
cmd.CommandText = #"
INSERT INTO TMP_TABLE
(
""keyColumn"",
""valueColumn""
)
VALUES (
:keyColumn,
:valueColumn)";
cmd.Parameters.Add( keyColumn );
cmd.Parameters.Add( valueColumn );
cmd.ArrayBindCount = values.Length;
var insertCount = await cmd.ExecuteNonQueryAsync();
cmd = connection.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "dbo.stored_procedure";
var result = await cmd.ExecuteNonQueryAsync();
transaction.Commit();
}
I created the temp table like this:
CREATE
GLOBAL TEMPORARY TABLE "dbo"."TMP_TABLE"
ON COMMIT DELETE ROWS
AS SELECT * FROM "dbo"."REAL_TABLE" WHERE 0=1;
And changed my stored procedure to use it:
CREATE OR REPLACE PROCEDURE stored_procedure AS
BEGIN
REM use the "dbo"."TMP_TABLE"
END;
This answer helped me with the approach of bulk inserting with one array per column. The thread also contains some further discussion about the topic and a more generic approach.

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

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)

OCI: How to bind output parameter of object type

I'm trying to call stored procedure (on oracle db 11g) from OCI based client. The procedure contains single OUT parameter of object type.
The problem: I always get "ORA-21525: attribute number or (collection element at index) %s violated its constraints" error.
I would highly appreciate if someone could give me a hint what can be the reason.
Notice: Interestingly, everything works ok in the following cases:
I replace return type from object type to nested table of such objects.
I replace return type to some primitive type, e.g. NUMBER
I make this parameter direction IN and bind it the same way.
Also, I discovered the following things:
The same error present, no matter if I return the result as OUT parameter from the procedure or if I user RETURN from FUNCTION.
If I try to call the stored procedure via PLSQL script, everything goes as expected without errors.
I also tried to create "parallel" C++ struct made of two OCINumber fields and use its object instead of calling OCIObjectNew(), but get the same error.
I also tried to set pOutParam = NULL, and bind it, but then I got "access violation, reading from location 00000".
Here is the code. PLSQL object type:
CREATE OR REPLACE TYPE dl_fake_type AS OBJECT
(
attr_one NUMBER(12,0),
attr_two NUMBER(12,0)
);
/
Procedure (for brevity I skip procedure declaration)
PROCEDURE dl_fake_fun(out_result OUT dl_fake_type)
IS
l_result dl_fake_type;
BEGIN
SELECT dl_fake_type(23, 35) INTO l_result FROM DUAL;
out_result := l_result;
END dl_fake_fun;
C++ OCI code (without connection initialization for brevity). Notice: I use MSVS2013 and some C++11 features, like std::string::front() method.
// ...
typedef basic_string<OraText, char_traits<OraText>, allocator<OraText> > OraTextString;
typedef basic_ostringstream<OraText, char_traits<OraText>, allocator<OraText> > OraOStringStream;
// Check if ociStatus == OCI_SUCCESS. If not, then print error and assert.
void checkOciStatus(const sword ociStatus, OCIError * errorHandle = NULL);
// ...
const OraTextString DL_FAKE_TYPE = (OraText const *)"DL_FAKE_TYPE";
const OraTextString outParamName = (OraText const *)":out_param";
OraOStringStream query(ios::ate);
query << "BEGIN my_dummy_pkg.dl_fake_fun(" << outParamName << "); END;";
const OraTextString & queryString = query.str();
OCIStmt * statement;
const ub4 executionMode = OCI_DEFAULT;
checkOciStatus(OCIHandleAlloc(envhp, (void **)&statement, OCI_HTYPE_STMT, /* xtramemsz */ 0, /* usrmempp */ NULL), errhp);
checkOciStatus(OCIStmtPrepare(statement, errhp, queryString.c_str(), queryString.length(), OCI_NTV_SYNTAX, executionMode), errhp);
const OraTextString schemaName = (OraText const *)"MY_SCHEMA_NAME";
OCIType * typeDescriptor = NULL;
checkOciStatus(
OCITypeByName(
envhp,
errhp,
svchp,
schemaName.c_str(),
schemaName.size(),
DL_FAKE_TYPE.c_str(),
DL_FAKE_TYPE.length(),
/* version name */ NULL,
/* version name length */ 0,
OCI_DURATION_SESSION,
OCI_TYPEGET_HEADER,
&typeDescriptor
),
errhp
);
OCIBind* bindHandle = NULL;
checkOciStatus(OCIBindByName(statement, &bindHandle, errhp, outParamName.c_str(), outParamName.length(), NULL, 0, SQLT_NTY, NULL, NULL, NULL, 0, NULL, executionMode), errhp);
void * pOutParam = NULL;
checkOciStatus(OCIObjectNew(envhp, errhp, svchp, OCI_TYPECODE_REF, typeDescriptor, NULL, OCI_DURATION_DEFAULT, /* true = value, false = ref */ FALSE, &pOutParam), errhp);
checkOciStatus(OCIBindObject(bindHandle, errhp, typeDescriptor, &pOutParam, NULL, NULL, 0), errhp);
checkOciStatus(OCIStmtExecute(svchp, statement, errhp, 1, 0, NULL, NULL, executionMode), errhp);
cout << "executed." << endl;
// ...
Thanks in advance.
After a period of struggling I've found the solution, which worked for me.
There were two points, which are vital for successful binding:
Object, which is intended to be bound have to be created by call to OCIObjectNew() as it is done in the code provided with the question. Notice: I also considered option to create instance of the struct representing corresponding C++ type directly. But this did not work even if the second condition was met.
After the object is created, all of its fields have to be initialized. I.e. fields of type OCINumber have to be set to some specific value (e.g. with OCINumberFromInt()) and pointers to OCIString have to be set to NULL.
Another gocha of output object binding (also observed when binding output nested tables) is that the second pointer to object passed to OCIBindObject is second pointer for reason: it seems, that OCI considers it as array of pointers to output objects. This means, that variable containing first pointer (to which points second pointer) must be valid until OCIExecuteStatement is called. This meens, that if you want to extract binding process to separate function, than you have to pass there second pointer to the object, which you then pass to the OCIBindObject(). Otherwise it will not work.
I hope, that those hints will help someone to avoid hours of frustration and painful struggling through the jungle of OCI (and Oracle DB) documentation.

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.

Resources