I have written this package
CREATE OR REPLACE
PACKAGE CURVES AS
type t_forecast_values is table of test.column2%TYPE index by varchar(20);
assoc_array t_forecast_values;
procedure sp_insert (param1 in varchar2, param2 in number);
END CURVES;
create or replace
package body curves as
procedure sp_insert (param1 in varchar2, param2 in number) as
begin
assoc_array(param1) := param2;
DECLARE primarykey NUMBER(10);
BEGIN
FOR i IN 1..2
LOOP
SELECT seq_curves.nextval INTO primarykey FROM dual;
INSERT INTO TEST (column1, column2, column3)
VALUES (primarykey, param1, assoc_array(param1));
INSERT INTO TEST2 (column1, column2)
VALUES (primarykey, 'default');
END LOOP ;
END;
end sp_insert;
end curves;
I call the procdedure through this c# code:
class Program
{
private static string connString = @"User Id=User; Password=root; Data Source=SOURCE";
private static List<string> forecast_types = new List<string> { "a", "b" };
private static List<double> forecast_values = new List<double> { .1, .2 };
static void Main(string[] args)
{
using (var con = new OracleConnection(connString))
{
string query = "BEGIN curves.sp_insert(:forecast_types, :forecast_values); END;";
try
{
con.Open();
using (OracleCommand cmd = con.CreateCommand())
{
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.BindByName = true;
cmd.Parameters.Add(new OracleParameter
{
ParameterName = ":forecast_types",
OracleDbType = OracleDbType.Varchar2,
Value = forecast_types.ToArray(),
Direction = ParameterDirection.Input,
CollectionType = OracleCollectionType.PLSQLAssociativeArray
}
);
cmd.Parameters.Add(new OracleParameter
{
ParameterName = ":forecast_values",
OracleDbType = OracleDbType.Double,
Value = forecast_values.ToArray(),
Direction = ParameterDirection.Input,
CollectionType = OracleCollectionType.PLSQLAssociativeArray
}
);
cmd.ExecuteNonQuery();
}
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex);
}
finally
{
con.Close();
}
}
}
}
i get the following error:
Oracle.DataAccess.Client.OracleException ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'SP_INSERT'
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'SP_INSERT'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored at Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, String procedure, Boolean bCheck)
at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, OracleConnection conn, String procedure, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, Boolean bCheck)
at Oracle.DataAccess.Client.OracleCommand.ExecuteNonQuery()
at OracleTest.Program.Main(String[] args) in \\comp\user$\Visual Studio 2010\Projects\OracleTest\OracleTest\Program.cs:line 57
I have no idea to resolve the problem, yet. Any help is appreciated.
Your package is not accepting a PLSQL Associative array, but rather single elements. What you are trying to do, I believe, is do an array insert (which is pretty much packaging up in C# your code X times and bulk sending it to the database to do the work)
this link directly describes what you want to do:
http://www.oracle.com/technology/sample_code/tech/windows/odpnet/howto/arraybind/index.html
(I made a few changes to your code, namely
1 & 2 should fix your problem, while #3 is more of syntactic sugar
however, this code is untested but out to work
EDIT
I did notice that you did try to pass in an associative array (I see the creation of the type of the looping within the procedure).
This is not too difficult to do either.
On that note you are on the correct track.
you’ll need to change the package to have assoc. arrays passed in via params
CREATE OR REPLACE
PACKAGE CURVES AS
type t_forecast_values is table of test.column2%TYPE index by varchar(20);
assoc_array t_forecast_values;
and you will need to add a size param to the param list as such
to get a great example of this goto
ORACLE_HOME\odp.net\samples\2.x\AssocArray\AssocArray.cs
it does exactly what you like
all of the above code is untested but should get you on the correct track, either from array binding (first example) or passing in the pl/sql array (second example)