我正在尝试调用Oracle函数,该函数返回封装在表
中的类型输入对象
create or replace
type Z_TBL_STRUCTURE_CODE
AS OBJECT
(
PROJ_ID varchar2(50 BYTE)
);
输入表格
create or replace
type Z_TABLE_STRUCTURE_CODE AS TABLE of Z_TBL_STRUCTURE_CODE;
Oracle功能
create or replace
FUNCTION Z_TESTE_IN_FUNC
(
var_teste in varchar2
)
return Z_TABLE_STRUCTURE_CODE
AS
tab Z_TABLE_STRUCTURE_CODE;
BEGIN
EXECUTE IMMEDIATE
'SELECT
CAST(
MULTISET(
select count(*) into num from structure
where structure_code in ('|| var_teste ||')) as Z_TABLE_STRUCTURE_CODE)
into tab
from dual;';
dbms_output.put_line(var_teste);
return tab;
END Z_TESTE_IN_FUNC;
注意:请不要执行即时执行,只需进行测试。
注意2:我知道有一个选项可以避免类型(使用游标)但我还没有理解主题因此我会在回答时忽略了CURSOS,除非它是唯一可用的选项。
C#代码
public DataTable getTaskStartFinish()
{
OleDbConnection con = null;
OleDbDataReader reader = null;
try{
con = new OleDbConnection(ConfigurationManager.ConnectionStrings["OracleBD"].ConnectionString);
OleDbCommand cmd = new OleDbCommand("", con);
cmd.CommandText = "Z_TESTE_IN_FUNC";
cmd.CommandType = CommandType.StoredProcedure;
OleDbParameter retval = new OleDbParameter("retval", OleDbType.VarChar, 10);
retval.Direction = ParameterDirection.ReturnValue;
OleDbParameter inval = new OleDbParameter("inval", OleDbType.Variant, 50);
inval.Direction = ParameterDirection.Input;
inval.Value = "1";
cmd.Parameters.Add(retval);
cmd.Parameters.Add(inval);
con.Open();
reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
return dt;
}
catch(Exception ex)
{
throw new Exception("getTaskStartFinish error: " + ex.Message);
}
finally
{
if(con != null)
con.Close();
if(reader != null || !reader.IsClosed)
reader.Close();
}
}
我得到的当前错误如下: ORA-06550:第1行第13栏: PLS-00382:表达式类型错误 ORA-06550:第1行第7列: PL / SQL:忽略语句
我在网上看到过类似的例子,但都使用了ExecuteScalar()或者一些代码来处理程序或函数返回奇异值而不是表格。
答案 0 :(得分:0)
好吧,如果你想返回一个值我的建议是(编辑多行):
OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["OracleBD"].ConnectionString);
OleDbCommand cmd = new OleDbCommand("Z_TESTE_IN_FUNC", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@procValueName", "myValue");
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
conn.Open();
try
{
da.Fill(dt);
}
catch (Exception excp)
{
//Handle Exception
}
finally
{
conn.Close();
}
编辑2:尝试使用此功能
OleDbConnection con = new OleDbConnection(cntStr);
con.Open();
OleDbCommand cmd = new OleDbCommand("F_TESTFUNC", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OleDbParameter("retVal", OleDbType.VarChar, 11,ParameterDirection.ReturnValue, true, 0, 0, "retVal", DataRowVersion.Current,null);
cmd.Parameters.Add("ID", strID);
cmd.ExecuteScalar();
con.Close();