简体   繁体   English

将 postgresql 函数转换为 oracle

[英]converting postgresql functions to oracle

I need to support postgresql and oracle in the product that I work in. When we install our db on oracle, we do not give privilege of "create type".我需要在我工作的产品中支持 postgresql 和 oracle。当我们在 oracle 上安装我们的数据库时,我们没有给予“创建类型”的权限。 So I am unsure as to how I can convert following two cases to oracle:所以我不确定如何将以下两种情况转换为 oracle:

  1. Function That returns array返回数组的函数

     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;

Second case: function that accepts array of integers:第二种情况:接受整数数组的函数:

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

You could make use of Oracle's built-in collection type sys.odcinumberlist .您可以使用 Oracle 的内置集合类型sys.odcinumberlist

Your function is equivalent to.你的功能相当于。

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;
/  

You may use TABLE function in your query to select from this table.您可以在查询中使用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

For Oracle 12.2 and above, you won't need TABLE function, you can directly select from your function.对于 Oracle 12.2 及更高版本,您不需要TABLE函数,您可以直接从您的函数中进行选择。

select * FROM get_array_of_integers(3);

Second case could be something like this.第二种情况可能是这样的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM