c# OleDb Oracle函数和表类型

本文关键字:类型 函数 OleDb Oracle | 更新日期: 2023-09-27 18:08:41

我试图调用一个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;

甲骨文函数

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;

注意:请忽略EXECUTE IMMEDIATE,它只是一个测试。

注2:我知道有一个选项可以避免类型(使用游标),但我还没有能够理解这个主题,所以我希望你在回答时忽略光标,除非它是唯一可用的选项。

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: Statement ignored

我在网上看到过类似的例子,但都使用ExecuteScalar()或一些过程或函数的代码返回奇异值,但不返回表。

c# OleDb Oracle函数和表类型

如果您想返回单个值,我的建议是(为多行编辑):

        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:Try this for function
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();