我需要在我工作的产品中支持 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 及更高版本,您不需要TABLE
函数,您可以直接从您的函数中选择。
select * FROM get_array_of_integers(3);
第二种情况可能是这样的。
CREATE OR REPLACE FUNCTION some_function( params sys.odcinumberlist )
RETURN INTEGER
AS
BEGIN
RETURN 1;
END;
/
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句