将postgresql函数转换为oracle

将postgresql函数转换为oracle,oracle,postgresql,Oracle,Postgresql,我需要在我工作的产品中支持postgresql和oracle。当我们在oracle上安装数据库时,我们不会授予“创建类型”的特权。因此,我不确定如何将以下两种情况转换为oracle: 返回数组的函数 create or replace function get_array_of_integers(i_param int) returns integer[] as $body$ declare array_of_integers int[]; begin select

我需要在我工作的产品中支持postgresql和oracle。当我们在oracle上安装数据库时,我们不会授予“创建类型”的特权。因此,我不确定如何将以下两种情况转换为oracle:

  • 返回数组的函数

         create or replace function get_array_of_integers(i_param int)
     returns integer[] as
     $body$
     declare array_of_integers int[];
     begin
    
        select
           case 
              when i_param = 1 then
                 array[1]
              when i_param = 2 then
                 array[1, 2, 3, 4]
              when i_param = 3 then
                 array[5, 6]
              else
                 array[]::integer[]
        end into array_of_integers;
    
        return array_of_integers;
    
     end;
     $body$
     language 'plpgsql' volatile
     cost 100;
    
  • 第二种情况:接受整数数组的函数:

    create or replace function some_function(params int[])
     ...
    

    提前谢谢

    您可以使用Oracle内置的集合类型
    sys.odcinumberlist

    你的函数相当于

    CREATE OR REPLACE FUNCTION get_array_of_integers(i_param INT)
    RETURN sys.odcinumberlist
    AS
      array_of_integers sys.odcinumberlist := NEW sys.odcinumberlist() ; --Initialization
    BEGIN
        IF i_param = 1 THEN
          array_of_integers.EXTEND(1); 
          array_of_integers(1) := 1;
        ELSIF i_param = 2 THEN
          array_of_integers.EXTEND(4);--appends 4 null elements to a collection
          array_of_integers(1) := 1; --assign elements
          array_of_integers(2) := 2;
          array_of_integers(3) := 3;
          array_of_integers(4) := 4;
        ELSIF i_param = 3 THEN
          array_of_integers.EXTEND(2);
          array_of_integers(1) := 5;
          array_of_integers(2) := 6;
        END IF;
    
       RETURN array_of_integers;
    END;
    /  
    
    您可以在查询中使用
    TABLE
    函数从该表中进行选择

    SQL> select * FROM TABLE(get_array_of_integers(2));
    
    COLUMN_VALUE
    ------------
               1
               2
               3
               4
    
    SQL> select * FROM TABLE(get_array_of_integers(0));
    
    no rows selected
    
    对于Oracle 12.2及更高版本,您不需要
    函数,您可以直接从函数中进行选择

    select * FROM get_array_of_integers(3);
    
    第二种情况可能是这样的

     CREATE OR REPLACE FUNCTION some_function( params sys.odcinumberlist )
     RETURN INTEGER
     AS
     BEGIN
       RETURN 1;
     END;
     /