0

我想知道是否有办法做到这一点:

我有一个类型对象 A。我想在对象 A 声明中添加一个静态函数,该函数具有一个类型为对象 A 表的参数。

怎么能知道:

  1. 我不能做到这一点 :

    STATIC FUNCTION blabla (myList IN TABLE OF A) : 编译器不理解这里的表

  2. 我无法创建类型 xxx TABLE OF A,因为在创建此新类型时 A 未知

所以总结一下:

create or replace 
TYPE myObject AS OBJECT (
    STATIC FUNCTION blabla (myList IN TABLE OF myObject)  
);
4

1 回答 1

0

这是使用 ANYDATA 数据类型的可能解决方案:

CREATE OR REPLACE 
TYPE my_object AS OBJECT (
  some_number NUMBER,
  STATIC FUNCTION static_test_function(p_my_objects_tab IN OUT ANYDATA) RETURN NUMBER
);
/

CREATE TYPE my_object_tab_t AS TABLE OF my_object;
/

CREATE OR REPLACE
TYPE BODY my_object AS
  STATIC FUNCTION static_test_function(p_my_objects_tab IN OUT ANYDATA) RETURN NUMBER
  AS
    v_my_objects_tab my_object_tab_t;
    v_dummy NUMBER;
    v_result NUMBER := 0;
  BEGIN
    -- get the collection from ANYDATA object - v_dummy holds status (succes or not)
    v_dummy := p_my_objects_tab.GetCollection(v_my_objects_tab);

    -- as an example, loop through the list and sum up the some_number field
    FOR v_i IN v_my_objects_tab.FIRST..v_my_objects_tab.LAST
    LOOP
      v_result := v_result + v_my_objects_tab(v_i).some_number;
    END LOOP;

    RETURN v_result;
  END;
END;
/

DECLARE
  v_my_objects_tab my_object_tab_t;
  v_anydata_my_objects_tab ANYDATA;

  v_result NUMBER;
BEGIN
  -- create the collection and populate it with some sample data
  v_my_objects_tab := my_object_tab_t();

  v_my_objects_tab.EXTEND();
  v_my_objects_tab(v_my_objects_tab.LAST()) := my_object(5);

  v_my_objects_tab.EXTEND();
  v_my_objects_tab(v_my_objects_tab.LAST()) := my_object(10);

  -- create ANYDATA object from the collection
  v_anydata_my_objects_tab := ANYDATA.ConvertCollection(v_my_objects_tab);

  -- call the static function expecting ANYDATA parameter
  v_result := my_object.static_test_function(v_anydata_my_objects_tab);

  dbms_output.put_line(v_result);
END;
/

输出:15

于 2013-10-09T14:03:10.913 回答