0

我正在尝试调用一个 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;

注意:请忽略立即执行,它只是为了测试。

注意 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:语句被忽略

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

4

1 回答 1

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();
于 2013-04-11T09:58:58.047 回答