在尝试使用这个 oracle 包/过程时我做错了什么?
我已经编写了这个包,
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;
我通过此 c# 代码调用该过程:
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();
}
}
}
}
我收到以下错误:
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的包不接受 PLSQL 关联数组,而是接受单个元素。我相信,您想要做的是进行数组插入(这几乎是将您的代码打包在 C# 中 X 次,然后将其批量发送到数据库来完成工作)
此链接直接描述了您想要执行的操作:
http://www.oracle.com/technology /sample_code/tech/windows/odpnet/howto/arraybind/index.html
(我对您的代码进行了一些更改,即
1 和 2 应该可以解决您的问题,而 #3 更多的是语法糖,
但是,此代码未经测试但可以工作
编辑
我确实注意到您这样做了尝试传递一个关联数组(我看到在过程中创建了循环类型),
这也不是太难。
从这一点来看,你走在正确的轨道上。
您需要更改软件包才能拥有 assoc。通过 params 传入的数组
创建或替换
封装曲线为
type t_forecast_values 是 test.column2%TYPE 的表,由 varchar(20) 索引;
assoc_array t_forecast_values;
并且您需要将大小参数添加到参数列表中,
以获得此 goto 的一个很好的示例
ORACLE_HOME\odp.net\samples\2.x\AssocArray\AssocArray.cs
它完全按照您的要求执行
以上所有代码都未经测试,但应该让您走上正确的轨道,无论是从数组绑定(第一个示例)还是传入 pl/sql 数组(第二个示例)
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)
不确定这是否是问题所在,但我相信
OracleDbType.Double
将映射到数据库类型FLOAT
,而不是声明的NUMBER
第二个参数的类型。我认为您应该将第二个参数的类型设置为 此表。Not sure if this is the problem, but I believe the
OracleDbType.Double
would map to the database typeFLOAT
, notNUMBER
which is the declared type of the second parameter. I think you should be setting the type of the second parameter toOracleDbType.Decimal
per this table.