【问题标题】:c# call Oracle' s Procedure Return arrayc#调用Oracle的过程返回数组
【发布时间】:2016-08-26 22:17:53
【问题描述】:

我想将一个字符串数组传递给 oracle,并在数据库中获取所有不匹配的值。这是我的过程:

    TYPE myArray IS TABLE of varchar(50) INDEX BY PLS_INTEGER;
    create or replace PACKAGE BODY TEST AS

      procedure CheckExistL(L IN myArray, lotNotMatch OUT myArray) AS
      j number:=0;
      cnt number :=0;
      BEGIN
        FOR i IN 1..L.count LOOP
        select COUNT(*) INTO cnt  FROM myTable 
        WHERE L01 = L(i);
       if (cnt = 0)
       then 

        lotNotmatch(j):=L(i);
        j := j + 1;
       end if;
       END LOOP;

      END CheckExistL;

    END TEST;
and in my C# my code is

               using (OracleCommand cmd = connection.CreateCommand())
                {
                    // 
                    cmd.BindByName = true;
                    cmd.ArrayBindCount = myArray.Count();

                    cmd.CommandText = "TEST.CheckExistLPNDetailLottables";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.BindByName = true;
                    OracleParameter P_In = new OracleParameter("L", OracleDbType.Varchar2, 50);
                    P_In.Direction = ParameterDirection.Input;
                    P_In.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
                    P_In.ArrayBindSize = new int[L.Count()];
                    P_In.Size = myArray.Count();
                    P_In.Value = myArray;

                    cmd.Parameters.Add(P_In);

                    OracleParameter P_result = new OracleParameter("lotNotMatch", OracleDbType.Varchar2, 50);
                    P_result.Direction = ParameterDirection.Output;
                    P_result.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
                    P_result.Size = myArray.Count();
                    P_result.ArrayBindSize = new int[myArray.Count()];
                    cmd.Parameters.Add(P_result);
                    cmd.Parameters["L"].Value = myArray;

                    cmd.ExecuteNonQuery();
                    var arrNotMatch = cmd.Parameters["lotNotMatch"].Value;
                    }

我没有收到任何错误编译,什么时候 excuteNonQuery 我收到了来自 Oracle 的错误: ORA-06513: PL/SQL: PL/SQL 表的索引超出宿主语言数组的范围 ORA-06512: 在第 1 行 哪一个超出范围?网络中的一些参考提到添加 maxRowsSize,但我在 cmd 或参数属性中找不到它。

【问题讨论】:

  • Oracle中的数组好像是从1开始的,试试改j number:=1;
  • 感谢 Vijai,现在我遇到另一个错误:编译绑定长度与执行绑定长度不同

标签: c# oracle database-connection oracle-sqldeveloper


【解决方案1】:

试试这个,它编译好了,但是请测试一下。

CREATE TYPE myArray AS TABLE of varchar(50);
create or replace procedure CheckExistL(L IN myArray, lotNotMatch OUT myArray) AS
      j number:=1;
      cnt number :=0;
  BEGIN
      lotNotMatch := new myArray();
      lotNotMatch.EXTEND(L.count);

        FOR i IN 1..L.count 
        LOOP
          select COUNT(*) INTO cnt  FROM myTable 
          WHERE L01 = L(i);

          if (cnt = 0)   then 
            lotNotmatch(j):=L(i);
            j := j + 1;
          end if;
       END LOOP;

END CheckExistL;

【讨论】:

  • 我收到以下错误:Error(7,24): PLS-00222: no function with name 'MYARRAY' exists in this scope
猜你喜欢
  • 2013-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多