【问题标题】:Oracle Procedure with out parameter and ArrayBindCount/ArrayBindSize没有参数和 ArrayBindCount/ArrayBindSize 的 Oracle 过程
【发布时间】:2020-05-09 19:26:33
【问题描述】:

我正在测试一个简单的 Oracle 程序,但不知何故我遇到了一个错误,我不明白这是为什么。

using (var dbConnection = new OracleConnection(ConfigurationManager.ConnectionStrings["KWC"].ConnectionString))
            {
                using (var command = dbConnection.CreateCommand())
                {
                    var data = new List<Tuple<int, string, string, string>>
                    {
                        Tuple.Create(1, "hello", "kitty", "empty1"),
                        Tuple.Create(2, "mike", "perry", "empty2")
                    };

                    int listCount = data.Count;
                    command.CommandText = "KWC_Test_Output";
                    command.CommandType = CommandType.StoredProcedure;
                    command.BindByName = true;
                    command.ArrayBindCount = listCount;

                    command.Parameters.Add("string1", OracleDbType.Varchar2, data.Select(t=>t.Item2).ToArray(), ParameterDirection.Input);
                    command.Parameters.Add("string2", OracleDbType.Varchar2, data.Select(t=>t.Item3).ToArray(), ParameterDirection.Input);
                    var returnParam = new OracleParameter("result", OracleDbType.Varchar2, null, ParameterDirection.Output) { ArrayBindSize = new int[2] { 255, 255 } };
                    command.Parameters.Add(returnParam);

                    //command.Parameters[2].ArrayBindSize = new int[data.Count()];
                    //command.Parameters[2].ArrayBindSize[0] = 100;
                    //command.Parameters[2].ArrayBindSize[1] = 100;

                    var myParams = command.Parameters; // for debugging

                    dbConnection.Open();
                    command.ExecuteNonQuery(); // gets exception here

                    Console.WriteLine("returned value from oracle procedure: " + command.Parameters["result"].Value);
                }
            }

我得到的错误是:

Oracle.ManagedDataAccess.Client.OracleException: 'ORA-06550: linje 1, kolonne 7: PLS-00306:调用“KWC_TEST_OUTPUT”时参数的数量或类型错误 ORA-06550: 林杰 1, 科隆 7: PL/SQL:语句被忽略'

这是程序:

CREATE OR REPLACE PROCEDURE KWC_Test_Output(p_string1 IN VARCHAR2, p_string2 IN VARCHAR2, result OUT VARCHAR2)
  AS
  BEGIN
    IF p_string1 = 'exception' THEN
      DBMS_OUTPUT.PUT_LINE(p_string1);
      DBMS_OUTPUT.PUT_LINE(p_string2);
      RAISE_APPLICATION_ERROR(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
    ELSE
        result := CONCAT(p_string1, p_string2);
        DBMS_OUTPUT.PUT_LINE(result);
    END IF;

    EXCEPTION
      WHEN OTHERS THEN
        result := SQLERRM;
        DBMS_OUTPUT.PUT_LINE(result);
END KWC_Test_Output;

谁能告诉我为什么它失败了?提前致谢!

【问题讨论】:

  • 如果我正确阅读了您的代码(不确定),它看起来像脚本 int INTEGER 数组中的第三个参数(返回参数),而 plsql 过程需要字符串(varchar2)。也是整数数组的格式,如 plsql 中需要一个集合类型。
  • 第三个参数为空。您看到的数组是绑定的配置。
  • 我想知道我是否应该使用 PL/SQL 关联数组。

标签: c# .net oracle plsql procedure


【解决方案1】:

如果您有合适的 TYPE 定义,则可以传递数组。

create type kwc_result is table of varchar2(100); 
create or replace procedure kwc_test_output(
          p_string1 in varchar2
        , p_string2 in varchar2
        , result   out kwc_result
        )
  as 
  ...  
end kwc_test_output;

我猜在 Oracle 术语中这实际上是一个嵌套表。但是由于您的程序当前是不必要的,所以一个简单的字符串就足够了。我想它可能是用于演示目的的精简版。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 2013-11-02
    相关资源
    最近更新 更多