如何在不使用数组的情况下在 postgres 函数中传递值列表
How do I pass a list of values in postgres function without using array
我需要创建一个函数,其中输入参数是具有单列的值列表:
Column1
Abc
Xyz
Mno
Create or replace function dummy(my_list <list of 3 values>) returns refcursor as
.......
$$
Open ref for select * from table1 where col1 in (my_list);
Return ref
End;
谁能告诉我如何在不使用数组的情况下做到这一点?
另外请告诉我如何在 pgAdmin 中调用它。
编辑:
下面是我正在尝试的代码:
CREATE TYPE public.row1 AS
(col1 character varying);
CREATE OR REPLACE FUNCTION public.xyz(
i_a character varying,
i_b row1[] DEFAULT array[NULL]::row1[])
RETURNS refcursor
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
DECLARE
ref refcursor='o_c'; -- Declare a cursor variable
BEGIN
OPEN ref FOR
select * from es_forecast_period_ngf a where a.fiscal_quarter_name=i_a and a.fiscal_period in any(i_b.col1);
RETURN ref; -- Return the cursor to the caller
END;
$BODY$;
calling mechanism :
BEGIN;
SELECT xyz('Q3 FY2031',array[row('Q3-M1'),row('Q3-M2')]::row1[]);
FETCH ALL in "o_c";
COMMIT;
error while compiling:
ERROR: syntax error at or near "any"
LINE 13: ... a.fiscal_quarter_name=i_a and a.fiscal_period in any(i_b.co...
^
SQL state: 42601
Character: 375
您可以使用可变参数函数(此示例使用 integer
并用 SQL 编写,但这没有区别):
CREATE FUNCTION dummy(VARIADIC args integer[]) RETURNS bigint
LANGUAGE sql AS
'SELECT count(*)
FROM generate_series(1, 9) AS i
WHERE i = ANY (args)';
SELECT dummy(1, 5, 12);
dummy
-------
2
(1 row)
SELECT dummy(1, 2, 5, 6, 9);
dummy
-------
5
(1 row)
可以使用任意数量的参数调用这样的函数,但它们都必须具有相同的类型(除非您想编写 C 代码)。
传递给这种可变参数的值(必须是参数列表中的最后一个)在函数体内形成一个数组。
另一个想法是将值列表作为可以转换为数组的 text 传递。数组语法的详细信息 here.
假设列表由文本值组成,则:
create or replace function dummy(my_list text) returns refcursor as
........
open ref for select * from table1 where col1 = any(('{'||my_list||'}')::text[]);
在这里,您始终将值列表作为文本传递。
如果是整数值,则 ('{'||my_list||'}')::text[]
变为 ('{'||my_list||'}')::integer[]
,对于其他数据类型依此类推。另请注意,值列表文本需要正确转义。
我需要创建一个函数,其中输入参数是具有单列的值列表:
Column1
Abc
Xyz
Mno
Create or replace function dummy(my_list <list of 3 values>) returns refcursor as
.......
$$
Open ref for select * from table1 where col1 in (my_list);
Return ref
End;
谁能告诉我如何在不使用数组的情况下做到这一点?
另外请告诉我如何在 pgAdmin 中调用它。
编辑: 下面是我正在尝试的代码:
CREATE TYPE public.row1 AS
(col1 character varying);
CREATE OR REPLACE FUNCTION public.xyz(
i_a character varying,
i_b row1[] DEFAULT array[NULL]::row1[])
RETURNS refcursor
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
DECLARE
ref refcursor='o_c'; -- Declare a cursor variable
BEGIN
OPEN ref FOR
select * from es_forecast_period_ngf a where a.fiscal_quarter_name=i_a and a.fiscal_period in any(i_b.col1);
RETURN ref; -- Return the cursor to the caller
END;
$BODY$;
calling mechanism :
BEGIN;
SELECT xyz('Q3 FY2031',array[row('Q3-M1'),row('Q3-M2')]::row1[]);
FETCH ALL in "o_c";
COMMIT;
error while compiling:
ERROR: syntax error at or near "any"
LINE 13: ... a.fiscal_quarter_name=i_a and a.fiscal_period in any(i_b.co...
^
SQL state: 42601
Character: 375
您可以使用可变参数函数(此示例使用 integer
并用 SQL 编写,但这没有区别):
CREATE FUNCTION dummy(VARIADIC args integer[]) RETURNS bigint
LANGUAGE sql AS
'SELECT count(*)
FROM generate_series(1, 9) AS i
WHERE i = ANY (args)';
SELECT dummy(1, 5, 12);
dummy
-------
2
(1 row)
SELECT dummy(1, 2, 5, 6, 9);
dummy
-------
5
(1 row)
可以使用任意数量的参数调用这样的函数,但它们都必须具有相同的类型(除非您想编写 C 代码)。
传递给这种可变参数的值(必须是参数列表中的最后一个)在函数体内形成一个数组。
另一个想法是将值列表作为可以转换为数组的 text 传递。数组语法的详细信息 here.
假设列表由文本值组成,则:
create or replace function dummy(my_list text) returns refcursor as
........
open ref for select * from table1 where col1 = any(('{'||my_list||'}')::text[]);
在这里,您始终将值列表作为文本传递。
如果是整数值,则 ('{'||my_list||'}')::text[]
变为 ('{'||my_list||'}')::integer[]
,对于其他数据类型依此类推。另请注意,值列表文本需要正确转义。