ORA-21700:对象不存在或被标记为删除关联数组,作为从ODP.NET调用的输入参数

时间:2019-02-07 18:43:45

标签: .net oracle associative-array odp.net

我在Oracle 12c上有此程序包代码

CREATE OR REPLACE PACKAGE Support_Data_Types AS
  TYPE ttDate            IS TABLE OF DATE  
END Support_Data_Types;

PROCEDURE GetData
(
    tabDates        IN SUPPORT_DATA_TYPES.TTDATE,
)

AS
BEGIN

    SELECT count(*) INTO n FROM table(tabDates);

END GetData;

如果我使用PL/SQL代码进行调用,

declare
    dates SUPPORT_DATA_TYPES.TTDATE;
begin
    dates(1) := To_DATE('12/31/2005','MM/DD/YYYY');
    dates(2) := To_DATE('03/31/2006','MM/DD/YYYY');
    dates(3) := To_DATE('06/30/2006','MM/DD/YYYY');
    dates(4) := To_DATE('09/30/2006','MM/DD/YYYY');

    MyPackage.GETVALUE(dates);
end;

但是,如果我从ODP.NET调用它,那么我会在行SELECT count(*) INTO n FROM table(tabDates);上收到错误消息

  

ORA-21700:对象不存在或被标记为删除

我的.NET代码

    Public Function GetValue(dates As IEnumerable(Of Date))

        Using connection As IDbConnection = Odp.ConnectionBuilder.CreateDatabaseConnection()
            Using cmd As OracleCommand = New OracleCommand
                cmd.Connection = DirectCast(connection, OracleConnection)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.CommandText = "MyPackage.GETVALUE"
                cmd.BindByName = True

                Using datesParam As OracleParameter = New OracleParameter()
                    datesParam.ParameterName = "tabDates"
                    datesParam.OracleDbType = OracleDbType.Date
                    datesParam.CollectionType = OracleCollectionType.PLSQLAssociativeArray
                    datesParam.Size = dates.Count()
                    datesParam.Direction = ParameterDirection.Input
                    datesParam.Value = dates.ToArray()
                    cmd.Parameters.Add(datesParam)
                End Using

                cmd.ExecuteNonQuery()

            End Using
        End Using
    End Function

如果将我与Oracle的sample code进行比较,我不确定这里出了什么问题。

有人有见识吗?

1 个答案:

答案 0 :(得分:-1)

经过几天的搜索,我以这种方式更改了代码:

CREATE OR REPLACE PACKAGE Support_Data_Types AS
            TYPE ttDate            IS TABLE OF DATE
END Support_Data_Types;
PROCEDURE GetData
(
    tabDates IN SUPPORT_DATA_TYPES.TTDATE,
)
AS
    v_temp SUPPORT_DATA_TYPES.TTDATE:= tabDates;  -- assigned the parameter to a temporary variable
BEGIN
    SELECT count(*) INTO n FROM table(v_temp);
END GetData;

我唯一要做的就是使用看起来很多余的v_temp。但它有效。我进行了此更改是因为我搜索了这篇文章here ...它提到了:

  

但是请注意,从12.1开始,您不能直接在TABLE运算符内部调用table函数。您必须在PL / SQL中调用它,将结果分配给一个变量,然后在TABLE中引用该变量。

尽管在我的情况下情况有所不同(我使用的是12.2),但它解决了我的问题。