SELECT FROM package-user-defined TABLE上的ORA-21700

时间:2015-07-06 10:52:21

标签: plsql package user-defined-types

我正在创建一个Oracle包(MyPackage),其中有一个自定义类型记录(ObjDataCollection)的公共自定义类型表(ObjData),它将用作IN其中一个Package函数(Calculate)的参数:

CREATE OR REPLACE PACKAGE MyPackage AS
    TYPE ObjData IS RECORD (
        t date NOT NULL := DATE '0001-01-01',
        v number(9, 4)
    );
    TYPE ObjDataCollection IS TABLE
        OF ObjData;
    FUNCTION Calculate(
        DataSource IN ObjDataCollection
    ) RETURN number;
END MyPackage;

CREATE OR REPLACE PACKAGE BODY MyPackage AS
    FUNCTION Calculate(
        DataSource IN ObjDataCollection
    ) RETURN number IS
        res number(9, 4) := 0;
        dateStart date;
        dsv ObjData;
        CURSOR q1 (dt date) IS
            SELECT * FROM TABLE(DataSource) --Throws ORA-21700: Object does not exist or is marked for delete oracle.
            WHERE t >= dt
            ORDER BY t;
    BEGIN
        -- some irrelevant code
        dateStart := DATE '2015-01-01';
        OPEN q1(dateStart);
        LOOP FETCH q1 INTO dsv;
            EXIT WHEN q1%NOTFOUND;
            res := res + dsv.v;
            -- some irrelevant code
        END LOOP;
        CLOSE q1;
        -- some irrelevant code
        return res;
    END Calculate;
END MyPackage;

我重新编写了我的代码,我在游标的第二行得到错误(在代码中标记):

  

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

我正在使用此数据执行我的包:

CREATE TABLE TestTable (d date DEFAULT DATE '0001-01-01', v number(9, 4));
INSERT INTO TestTable VALUES (DATE '2015-01-01', 2.1);
INSERT INTO TestTable VALUES (DATE '2015-01-08', 3.1);
INSERT INTO TestTable VALUES (DATE '2015-01-15', 4.1);
INSERT INTO TestTable VALUES (DATE '2015-01-22', 5.1);
INSERT INTO TestTable VALUES (DATE '2015-01-29', 6.1);
INSERT INTO TestTable VALUES (DATE '2015-02-05', 7.1);

此代码运行测试:

CREATE OR REPLACE PROCEDURE TestMyPackage AS
    res MyPackage.ObjDataCollection;
    counter number(9, 4);
BEGIN
    SELECT d, v
        BULK COLLECT INTO res
        FROM TestTable
        ORDER BY v;
    counter := MyPackage.Calculate(res);
END TestMyPackage;

为什么我收到这个ORA-21700例外?

3 个答案:

答案 0 :(得分:0)

PACKAGE BODY MyPackage AS
    FUNCTION Calculate(
        DataSource IN ObjDataCollection
    ) RETURN number IS
        res BINARY_FLoAT:= 0;
        dateStart date;
        dsv ObjData;
        copy_of_DataSource ObjDataCollection;

        procedure sortCollection(toSort in out ObjDataCollection)
        is
          type idx_coll  is table of ObjData;
          type sort_help is table of idx_coll index by varchar2(16 char);
           v_sort sort_help;
           v_temp varchar2(16);
           v_result ObjDataCollection := new ObjDataCollection();
           v_cnt PLS_INTEGER := 0;
        begin
          for i in nvl(toSort.first,1) .. nvl(toSort.last,-1) loop
              v_temp := to_char(toSort(i).t,'yyyymmddhh24miss');
              if v_sort.exists(v_temp) then
                 v_sort(v_temp).extend(1);
                 v_sort(v_temp)(v_sort(v_temp).count) := toSort(i);
              else
                 v_sort(v_temp) := idx_coll(toSort(i));
              end if;
          end loop;
          v_result.extend(toSort.count);
          v_temp := v_sort.first;
          while v_temp is not null loop
              for i in nvl(v_sort(v_temp).first,1) .. nvl(v_sort(v_temp).last,-1) loop
                 v_cnt := v_cnt +1;
                 v_result(v_cnt) := v_sort(v_temp)(i);
              end loop;
              v_temp := v_sort.next(v_temp);
          end loop;
          toSort := v_result;
        end;

    BEGIN
        -- some irrelevant code
        copy_of_DataSource := DataSource;
        dateStart := DATE '2015-01-01';
        sortCollection(copy_of_DataSource);
        for i in nvl(copy_of_DataSource.first,1) .. nvl(copy_of_DataSource.last,-1) loop
          if copy_of_DataSource(i).t > dateStart then
             res := res +  copy_of_DataSource(i).v;
             dbms_output.put_line(copy_of_DataSource(i).t);
          end if;
        end loop;
        -- some irrelevant code
        return res;
    END Calculate;
END MyPackage;

答案 1 :(得分:0)

由于日期是一项任务,我最终通过在他的一条评论中按照建议Arkadiusz Łukasiewicz在模式级别创建用户定义的类型和表来修复它。谢谢你的努力。

由于不是我想要的,我不会将任何答案标记为正确。

答案 2 :(得分:0)

通过以下步骤解决了相同的问题:

  1. 拖放定义类型的包
  2. 使用该类型的包裹
  3. 编译具有类型的包
  4. 编译目标包

我在具有类型/常量的包中添加了新的常量之后得到了ORA-21700。我想依赖项存在一些内部问题,但我没有做很多事情。